Selection Sort - CS50 Shorts

preview_player
Показать описание
***

This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

***

HOW TO SUBSCRIBE

HOW TO TAKE CS50

HOW TO JOIN CS50 COMMUNITIES

HOW TO FOLLOW DAVID J. MALAN

***

CS50 SHOP

***

LICENSE

CC BY-NC-SA 4.0
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License

David J. Malan
Рекомендации по теме
Комментарии
Автор

My Python code for this sorting algorithm:

arr = [7, 6, 8, 2, 3, 1, 4, 5]

def selection_sort(arr):

start = 0
min = None; min_index = None

while(start < len(arr) - 1):

min = arr[start]
min_index = start

for i in range(start + 1, len(arr)):

if(min > arr[i]):
min = arr[i]
min_index = i

temp = arr[min_index]
arr[min_index] = arr[start]
arr[start] = temp
start += 1

return arr


print('Before Selection Sort : ', arr)
print('After Selection Sort : ', selection_sort(arr))

rafatulalam
Автор

guys, you are so great. wish i could be someone saying "this is cs50".

annakudriavtseva
Автор

Instead of 0(n^2) and Ω(n^2), could the answer just be Θ(n^2), since that would cover the best and worst case?

Slimgoodie
Автор

I thought it would take 2 minutes to code this but I got way too used to python.
:/

TheRedstoneTaco
Автор

The pause when waiting for us was disturbing

allthingsthoughchris
Автор

function SelectionSort(array){
for(var i = 0; i < array.length; i++){

console.log("at value => ", array[i])
var smallest = array[i];
for(var j = i; j < array.length; j++){
j)
console.log("testing " +array[i] + " with "+ array[j])
if(smallest <= array[j]){
console.log(array[i] + "value is already smaller or d same")
console.log(" Not swapping")
continue;
}
console.log("value is NOT smaller, swapping and continueing to next value");
var old = array[i];
array[i] = array[j];
array[j] = old;
console.log("swaped, smallest is currently" + array[i])
console.log(array)
}
// array[i] = smallest;
console.log("swapping")
console.log(array)
}
console.log("final =>" + array)
}

SelectionSort([5, 4, 33, 2, 33, 33, 2, 1, 100, 34, 45]);
// How did i do?

gomezmario.f
Автор

So I make the array I search for smaller in each step right? So I don't search in the already sorted part (and same in bubble sort?)

andreasv
Автор

do you need pointers to implement this

rxtez
Автор

sorry, why is it not n!, ie n(n-1)(n-2)...

brandybabyyy
Автор

brain.exe stopped working








I did understand eventually though

raywu
Автор

Python:

def (v):

l = 0
r = len(v)

for i in range(l, r):

for j in range(i+1, r):

print(v[i], '>', v[j], v[i] > v[j])

if v[i] > v[j]:

v[i], v[j] = v[j], v[i]
print(v)


return v

JorgeArmandoBM
Автор

Doug how could you be so prejudice!? You're all about blue, red, and green but what about all of the other colors. Why do you hate purple?! Who hurt you Doug and what does purple have to do with it?

lowrl