Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 88

Design and Analysis of

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.
...

◦ Repeat until all values are in their proper places.


Selection sort example
Initial array:
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value 22 18 12 -4 27 30 36 50 7 68 91 56 2 85 42 98 25

After 1st, 2nd, and 3rd passes:


index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 18 12 22 27 30 36 50 7 68 91 56 2 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 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.

public static void selectionSort(int[] a) {


for (int i = 0; i < a.length - 1; i++) {

// find index of smallest remaining value


int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}

// swap smallest value its proper place, a[i]


swap(a, i, min);
}
}
Algorithm of Selection Sort
The time complexity of selection sort
 The analysis of selection sort is straightforward. The input size is
given by the number of elements n; the basic operation is the key
comparison A[j] < A[min]. The number of times it is executed
depends only on the array size and is given by the following sum:

 Thus, selection sort is a O(n2) algorithm on all inputs.


The best case of sorting is like:
1,2,3,4,5,6,7
The worst case of sorting is like:
7,6,5,4,3,2,1

But with the selection sort method, the time


complexity for each case is the same, because
whatever the array is looks like, the function
will go through all the values in it. so the
number of Comparisons and assignments is
independent of the data.
The time complexity of selection sort
When there is n value is the array, then
during the first time, the function executes n-
1 times, at the second time, it executes n-2
times…
so the time need to sort is n-1+n-2+…+1=
(n(n-1))/2, when n equal to infinite, it equals
n^2
So , the best, average and worst case time
complexities of the selection sort are all
O(n^2)
Complexity Analysis of Selection
Sort

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

Finished first “Bubble Up”

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

Finished second “Bubble Up”

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

Finished third “Bubble Up”

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

Finished fourth “Bubble Up”

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

Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98

0 1 2 3 4 5 6 7
Finished “Early”

We didn’t do any swapping,


so all of the other elements
must be correctly placed.

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;

for (i=0; i<N-1; i++)


{
for (int j=0; j<N-i-1; j++)
if (a[j] > a[j+1])
{ temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
Sorting An Array(Descending)
int a[10]={33,10,1,87,6,44,23,3,11,82};
int i,j,temp;
int N=10;

for (i=0; i<N-1; i++)


{
for (int j=0; j<N-i-1; j++)
if (a[j] < a[j+1])
{ temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
Optimized Bubble Sort:
int a[10]={33,10,1,87,6,44,23,3,11,82};
int i,j,temp;
int N=10; bool swap=true;
for (i=0; i<N-1; i++)
{
for (int j=0; j<N-i-1; j++)
if (a[j] > a[j+1])
{ temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
swap = false;
}
if(!swap)
break;
}
Complexity Analysis of Bubble Sort

Worst Case: O(n2)
Best Case: O(n)
Space Complexity: O(1)

You might also like