Professional Documents
Culture Documents
Algorithm 9th Lecture Sorting Type 1
Algorithm 9th Lecture Sorting Type 1
Algorithm
Sorting Algorithm
Sohail Ahmad
Barani Institute of Management Science
Sorting Algorithm
SelectionSort
Bubble Sort
Merge Sort
Quick Sort
Selection sort
selection sort: Orders a list of values by repeatedly
putting the smallest or largest unplaced value into its
final position.
The algorithm:
◦ Look through the list to find the smallest value.
◦ Swap it so that it is at index 0.
◦ Look through the list to find the second-smallest
value.
◦ Swap it so that it is at index 1.
...
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 12 22 27 30 36 50 7 68 91 56 18 85 42 98 25
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 22 27 30 36 50 12 68 91 56 18 85 42 98 25
Selection sort code
// Rearranges the elements into sorted order
using the selection sort algorithm.
Worst Case: O(n2)
Best Case: O(n2)
Space Complexity: O(1)
Bubble Sort
◦ Move from the front to the end
◦ “Bubble” the largest value to the end using
pair-wise comparisons and swapping
98 23 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
98 23 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
98 23 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 98 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
23 98 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 98 45 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 98 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
23 45 98 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 98 14 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 98 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
23 45 14 98 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 98 6 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 98 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
23 45 14 6 98 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 98 67 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 67 98 33 42
0 1 2 3 4 5 6 7
An Animated Example
23 45 14 6 67 98 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 67 98 33 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 67 33 98 42
0 1 2 3 4 5 6 7
An Animated Example
23 45 14 6 67 33 98 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 67 33 98 42
0 1 2 3 4 5 6 7
An Animated Example
Swap
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
After First Pass of Outer Loop
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
No Swap
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 45 14 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 45 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 14 45 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 45 6 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 6 45 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 14 6 45 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
No Swap
23 14 6 45 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 14 6 45 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 6 45 67 33 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 6 45 33 67 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
23 14 6 45 33 67 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 6 45 33 67 42 98
0 1 2 3 4 5 6 7
The Second “Bubble Up”
Swap
23 14 6 45 33 42 67 98
0 1 2 3 4 5 6 7
After Second Pass of Outer Loop
23 14 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
23 14 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
23 14 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 23 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
14 23 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 23 6 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 6 23 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
14 6 23 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
No Swap
14 6 23 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
14 6 23 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 6 23 45 33 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 6 23 33 45 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
14 6 23 33 45 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 6 23 33 45 42 67 98
0 1 2 3 4 5 6 7
The Third “Bubble Up”
Swap
14 6 23 33 42 45 67 98
0 1 2 3 4 5 6 7
After Third Pass of Outer Loop
14 6 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
14 6 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
Swap
14 6 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fourth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
After Fourth Pass of Outer Loop
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
The Fifth “Bubble Up”
No Swap
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
After Fifth Pass of Outer Loop
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
Finished “Early”
6 14 23 33 42 45 67 98
0 1 2 3 4 5 6 7
Sorting An Array(Ascending)
int a[10]={33,10,1,87,6,44,23,3,11,82};
int i,j,temp;
int N=10;
Worst Case: O(n2)
Best Case: O(n)
Space Complexity: O(1)