Docsity

Sorting-Algorithms and Data Structures-Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Sorting-Algorithms and Data Structures-Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Sorting 7-

Chapter 7

Sorting

EE 232 Data Structures Session-05 , Spring-

Sorting 7-

Chapter 7: Sorting

7.1 Preliminaries

7.2 Insertion Sort

7.3 A Lower Bound for Simple Sorting Algorithms

7.4 Shellsort

7.5 Heapsort

7.6 Mergesort

7.7 Quicksort

Sorting 7-

Chapter 7: Sorting

7.1 Preliminaries

7.2 Insertion Sort

7.3 A Lower Bound for Simple Sorting Algorithms

7.4 Shellsort

7.5 Heapsort

7.6 Mergesort

7.7 Quicksort

Sorting 7-

Preliminaries

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

Insertion Sort

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

Insertion Sort…

 Example

Sorting 7-

Chapter 7: Sorting

7.1 Preliminaries

7.2 Insertion Sort

7.3 A Lower Bound for Simple Sorting Algorithms

7.4 Shellsort

7.5 Heapsort

7.6 Mergesort

7.7 Quicksort

Sorting 7-

A Lower Bound for Simple

Sorting Algorithms

 Inversion

 An ordered pair (i, j) such that i < j but a[i] > a[j]

 Example

  • 34,8,64,51,32,
    • (34,8), (34, 32), (34, 21) , (64, 51), (64, 32), (64, 21), (51, 32), (51, 21), and (32, 21)

 This is exactly the number of swaps that needed to be performed by insertion sort  For a sorted array, number of inversions?

Sorting 7-

A Lower Bound for Simple

Sorting Algorithms…

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

Chapter 7: Sorting

7.1 Preliminaries

7.2 Insertion Sort

7.3 A Lower Bound for Simple Sorting Algorithms

7.4 Shellsort

7.5 Heapsort

7.6 Mergesort

7.7 Quicksort

Sorting 7-

Shellsort…

 Algorithm…

 After a phase, using some increment hk , for every

i , A[i]  A[i+hk] ; all elements spaced hk apart are

sorted

hk – sorted

Sorting 7-

Shellsort…

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-

Shellsort…

 Worst-case Analysis

 Theorem 1

  • The worst case running time of Shellsort, using Shell’s increments, is Q( N^2 )

 Theorem 2

  • The worst case running time of Shellsort, using Hibbard’s increments, is Q( N3/^2 ) - 1, 3, 7, …, 2k^ -

Sorting 7-

Chapter 7: Sorting

7.1 Preliminaries

7.2 Insertion Sort

7.3 A Lower Bound for Simple Sorting Algorithms

7.4 Shellsort

7.5 Heapsort

7.6 Mergesort

7.7 Quicksort