Skip to content

Instantly share code, notes, and snippets.

@thien
Last active September 16, 2020 06:15
Show Gist options
  • Save thien/8b94c98c290e7462bd905ba4baeb4e31 to your computer and use it in GitHub Desktop.
Save thien/8b94c98c290e7462bd905ba4baeb4e31 to your computer and use it in GitHub Desktop.
A recursive SelectionSort algorithm written in python
def selection(list, i, j, flag):
size = len(list)
if (i < size - 1):
if (flag):
j = i + 1;
if (j < size):
if (list[i] > list[j]):
list[i], list[j] = list[j], list[i]
selection(list, i, j + 1, 0);
selection(list, i + 1, 0, 1);
# print(list)
list = [6, 2, 3, 7, 9, 1, 4, 10, 8, 5]
selection(list, 0, 0, 1)
print(list)
@yanddam
Copy link

yanddam commented Jan 26, 2018

Hi thanks for this script. here is a revision:

def selection(list, i, j, flag):
    print("i=",i,"j=",j)
    size = len(list)
    if (i < size - 1):
        if (flag):
            j = i + 1;
        if (j < size):
            if (list[i] > list[j]):
            	list[i], list[j] = list[j], list[i]
            selection(list, i, j + 1, 0);
        else:
            selection(list, i + 1, i+2, 1);

list = [6, 2, 3, 7, 1]
selection(list, 0, 1, 1)
print(list)

@dking11
Copy link

dking11 commented Sep 16, 2020

what does the flag represent in the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment