You are on page 1of 18

•1

Sorting Algorithms
Selection Sort
•2

5 2 4 6 1 3

1
Selection Sort
•3

5 2 4 6 1 3

1 2
Selection Sort
•4

5 2 4 6 1 3

1 2 3
Selection Sort
•5

5 2 4 6 1 3

1 2 3 4
Selection Sort
•6

5 2 4 6 1 3

1 2 3 4 5
Selection Sort
•7

5 2 4 6 1 3

1 2 3 4 5 6
Selection Sort
•8
Selection Sort- passes
•9

89 45 68 90 29 34 17

17 45 68 90 29 34 89

17 29 68 90 45 34 89

17 29 34 90 45 68 89

17 29 34 45 90 68 89

17 29 34 45 68 90 89

17 29 34 45 68 89 90
•10
Algorithm to find minimum
•11

Algorithm MinElement (A[0….n-1])


//Input: An array of n elements
//Output: return the minimum element
min 0
for j 1 to n-1 do
If A[j] < A[min]
min j
return A[min]
Selection Sort
•12
Insertion sort
•13
Insertion sort
•14
Insertion Sort- passes
•15

89 45 68 90 29 34 17

45 89 68 90 29 34 17

45 68 89 90 29 34 17

45 68 89 90 29 34 17

29 45 68 89 90 34 17

29 34 45 68 89 90 17

17 29 34 45 68 89 90
Insertion Sort- passes
•16
Insertion sort algorithm
•17

Algorithm InsertionSort(A [0…n-1])


// Input: An array consisting of n elements
// Output: Sorted array
for i 1 to n-1 do
j=i;
while j>0 and A[j] <A[j-1]
swap A[j] and A[j-1]
j=j-1
Bubble Sort
•18

You might also like