
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Prof. Gangesh Charu delivered this lecture at Alagappa University for Data Structures course. Its main points are: Sorting, Insertion, Lower, Bound, Shellsort, Heapsort, Mergesort, Quicksort, Algorithms, Integer
Typology: Slides
1 / 56
This page cannot be seen from the preview
Don't miss anything!

















































Sorting 7-
EE 232 Data Structures Session-05 , Spring-
Sorting 7-
Sorting 7-
Sorting 7-
ļ± Each algorithm will be passed an array containing the elements and an integer containing the number of elements
ļ± We will assume that N , the number of elements passed to our sorting routines, has already been checked and is legal
ļ± Data will start at position 0 for all the sorts
Sorting 7-
ļ± The Algorithm
ļ¶ Insertion sort consists of N - 1 passes ļ¶ For pass P = 1 through N - 1 , insertion sort ensures that the elements in positions 0 through P are in sorted order
ļ¶ Insertion sort makes use of the fact that elements in positions 0 through P - 1 are already known to be in sorted order
ļ¶ In pass P , we move the element in position P left until its correct place is found among the first P + 1 elements
Sorting 7-
ļ± Example
Sorting 7-
Sorting 7-
ļ± Inversion
ļ¶ An ordered pair (i, j) such that i < j but a[i] > a[j]
ļ¶ Example
ļ¶ This is exactly the number of swaps that needed to be performed by insertion sort ļ¶ For a sorted array, number of inversions?
Sorting 7-
ļ± Theorem 2
ļ¶ Any algorithm that sorts by exchanging adjacent elements requires Ī©( N ²) average time
ļ¶ The average number of inversions is N(N-1)/4 = Ī©( N ²)
Sorting 7-
Sorting 7-
ļ± Algorithmā¦
ļ¶ After a phase, using some increment hk , for every
sorted
ļ¶ hk ā sorted
Sorting 7-
void Shellsort( ElementType A[ ], int N ) { int i, j, Increment; ElementType Tmp; /* 1/ for( Increment = N / 2; Increment > 0; Increment /= 2 ) / 2/ for( i = Increment; i < N; i++ ) { / 3/ Tmp = A[ i ]; / 4/ for( j = i; j >= Increment; j -= Increment ) / 5/ if( Tmp < A[ j - Increment ] ) / 6/ A[ j ] = A[ j - Increment ]; else / 7/ break; / 8*/ A[ j ] = Tmp; } }
Sorting 7-
ļ± Worst-case Analysis
ļ¶ Theorem 1
ļ¶ Theorem 2
Sorting 7-