Sort a List

Question

How do you sort a list in Python without using sort() or sorting algorithms ?

in progress 0
Puja 55 years 8 Answers 889 views Member 1

Answers ( 8 )

  1. Below is one of the way to sort number list . Please comment if you have any doubts in below approach.
    num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    sorted_list

  2. num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    sorted_list

  3. num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    print(sorted_list)

  4. Attached is the code you can use to sort array in ascending order . Please check and comment in case of any doubts

  5. Below is sample code to sort in ascending order .
    num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    print(sorted_list)

  6. // This enables debugging.
    define( ‘WP_DEBUG’, true );
    Below is sample code to sort in ascending order .
    num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    print(sorted_list)

  7. Below is simple code to sort number list in ascending order.
    num_list = [2,5,1,7,8]
    sorted_list = []
    while num_list:
    min_num = num_list[0] # taking first element as minimum
    for element in num_list: # loop through all elements
    if element < min_num:
    min_num = element
    sorted_list.append(min_num) # append minimum element
    num_list.remove(min_num) # remove it from original list
    print(sorted_list)

  8. D = [2,5,1,7,8]
    D.sort()

Leave an answer

Browse
Browse