Professional Documents
Culture Documents
Sorting
Sorting
Uzma Rifat
08/11/2015
THE SORTING PROBLEM
Input:
Output:
2
STRUCTURE OF DATA
3
WHY STUDY SORTING ALGORITHMS?
There are a variety of situations that we can
encounter
Do we have randomly ordered keys?
Are all keys distinct?
How large is the set of keys to be ordered?
Need guaranteed performance?
4
SOME DEFINITIONS
Internal Sort
The data to be sorted is all stored in the computers
main memory.
External Sort
Some of the data to be sorted might be stored in some
external, slower, device.
In Place Sort
The amount of extra space required to sort the data is
constant with the input size.
5
SORTING METHODS
7
INSERTION SORT
8
INSERTION SORT
9
INSERTION SORT
10
INSERTION SORT
Worst Case: O(n2)
Best Case: O(n)
Less efficient on larger lists
public static void Insertion Sort( int [ ] num)
{
int j; // the number of items sorted so far
int key; // the item to be inserted
int i;
13
Insert A[ j ] into the sorted sequence A[1 . . j -1]0 n-1
ij-1 c4 n-1
while i > 0 and A[i] > key c5
n
t
j 2 j
do A[i + 1] A[i] c6
n
(t j 1)
j 2
ii1 c7
n
(t j 1)
j 2
A[i + 1] key c8 n-1
tj: # of times the while statement is executed at iteration j
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n n n
j 2 j 2 j 2
COMPARISONS AND EXCHANGES IN
INSERTION SORT
INSERTION-SORT(A) cost times
for j 2 to n c1 n
do key A[ j ] c2 n-1
Insert A[ j ] into the sorted sequence A[1 . . j -
0 n-1
1]
n2/2 comparisons c4 n-1
ij-1
c5
n
j 2 j
t
while i > 0 and A[i] > key
c6
n
(t j 1)
do A[i + 1] A[i] j 2
n2/2 exchanges c7
n
(t j 1)
ii1 j 2
c8 n-1
A[i + 1] key
14
SELECTION SORT
A very simple Algorithm
Selects smallest or largest element and places it on the
head of the array
Complexity O(n2) for best and worst case
SELECTION SORT
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with the
element in the second position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the amount of order
in the file
16
EXAMPLE
8 4 6 9 2 3 1 1 2 3 4 9 6 8
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
17
SELECTION SORT
Alg.: SELECTION-SORT(A)
8 4 6 9 2 3 1
n length[A]
for j 1 to n - 1
do smallest j
for i j + 1 to n
do if A[i] < A[smallest]
then smallest i
exchange A[j] A[smallest]
18
for(int x=0; x<n; x++)
{
int index_of_min = x;
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
ANALYSIS OF SELECTION SORT
Alg.: SELECTION-SORT(A) cost times
n length[A]
c1 1
for j 1 to n - 1
c2 n
do smallest j
c3 n-1
for i j + 1 to n
n2/2
do if A[i] < A[smallest] c4 nj11 (n j 1)
comparisons
then smallest i c5
n 1
j 1
(n j )
n
exchanges
exchange A[j] A[smallest] c6
n 1
j 1
(n j )
c7 n-1
n 1 n 1 n 1
T (n) c1 c2 n c3 (n 1) c4 (n j 1) c5 n j c6 n j c7 (n 1) 20
(n 2 )
j 1 j 1 j 2
BUBBLE SORT
Smaller elements bubble to the top of the list
Complexity O(n) for best case
repeat
swapped = false
for i = 1 to n-1
swapped = true
end if
end for
end procedure
BUBBLE-SORT RUNNING TIME
Alg.: BUBBLESORT(A)
for i 1 to length[A] c1
do for j length[A] downto i + 1 c2
Comparisons: do if A[j] < A[j -1] c3
n2/2 then exchange A[j] A[j-1] c4
Exchanges: n2/2
n
(n i )
n n
T(n) = c1(n+1) + c2 (n i 1) c3 (n i) c4
i 1 i 1 i 1
n
= (n) + (c2 + c2 + c4) (n i )
i 1
n n n
n ( n 1) n 2
n
where (n i) n i n 2
i 1 i 1 i 1 2 2 2
Thus,T(n) = (n2)
25