user2970732
user2970732

Reputation: 13

sort list by using minimum value

This is what i did but it didn't work:

def min_sorted(xs):
    copy_list= xs
    list=[]
    while copy_list:
        minimum=copy_list[0]
        for i in copy_list:
            if i < minimum:
                minimum = i
            if not i == minimum:    
                list.append(minimum)
    print (list)

this is what i did but it didn't work like this can some one check for me please?

ex: xs = [7, 8, 3, 1, 5]

min_sorted([7, 8, 3, 1, 5]) ====> [1, 3, 5, 7, 8]

xs ====>[7, 8, 3, 1, 5]

Upvotes: 0

Views: 4434

Answers (6)

Albert-2323
Albert-2323

Reputation: 9

This is a soulution implemented in C, using the pattern called sorting by minimum values. It'a quite complicated, hard to understand, but let's give it a try!

 // 9.  SORTING BY MINIMUM VALUES

#include <stdio.h>

#define NUMBER 25   //SIZE OF THE ARRAY

int main() {
    int array[] = {56, 32, 45, 67, 723, 9, 7, 12, 545, 45, 1, 76, 345, 311, 6, 734, 87, 456, 325, 786, 34, 6, 5, 987, 3};

    for (int i = 0; i < NUMBER - 1; ++i) {

        int index = i;
        int value = array[i];

        for (int j = i + 1; j < NUMBER; ++j) {
            if (value > array[j]) {
                value = array[j];
                index = j;
            }
        }
        array[index] = array[i];
        array[i] = value;
    }

    for (int k = 0; k < NUMBER; k++) {
        printf("%d ", array[k]);
    }

    return 0;
}

Upvotes: -1

Mars
Mars

Reputation: 4995

The bubblesort pushes the minimum value to the front while iterating backwards through the list in the inner loop and iterating forward in the outer loop.

def min_sorted(xs):
    xsort = []
    for x in xs:
        xsort.append(x)
    i = 0
    while i < len(xsort)-1:
        for j in range(len(xsort)-1, i, -1):
            if xsort[j] < xsort[j-1]:
                y = xsort[j-1]
                xsort[j-1] = xsort[j]
                xsort[j] = y
        i = i + 1
    return xsort
xs = [7,8,3,1,5]
sorted = min_sorted(xs)
print(xs)
print(sorted)

OUTPUT:

[7, 8, 3, 1, 5, 9]
[1, 3, 5, 7, 8, 9]

Upvotes: 0

Jonathan Ruffin
Jonathan Ruffin

Reputation: 160

Based on your question history, this looks like a homework problem. I'll assume you're not allowed to use sorted, as jrd1 suggested. If you can, definitely use sorted instead.

Selection Sort sounds like what you want.

Upvotes: 2

jrd1
jrd1

Reputation: 10726

Since you want to sort by the minimum value, it's a much better idea to use the built-in sorted method which is designed precisely for this job, rather than your own custom version:

xs = [7, 8, 3, 1, 5]

def sort_list(lst):
    return sorted(lst)

print sort_list(xs)

This outputs:

[1, 3, 5, 7, 8]

This way, it also leaves your original list untouched, too.

NOTE:

As per @Ramchandra Apte:

Note that sorted() returns an iterator in Python 3, so if you iterate over that iterator, it will be finished and no more values can be produced from the iterator. So you might want to pass the output of sorted() to list().

Upvotes: 3

dbra
dbra

Reputation: 631

def min_sort(xs):
    oxs = []
    txs = list(xs)
    while txs:
        midx = 0
        for i, x in enumerate(txs[1:]):
            if x < txs[midx]:
                midx = i+1
        oxs.append(txs.pop(midx))
    return oxs

Upvotes: 2

Roberto
Roberto

Reputation: 2786

You can actually do:

xs = [7, 8, 3, 1, 5]
sorted_mintomax = sorted(xs)

Upvotes: 1

Related Questions