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

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

2011/2012

Uploaded on 07/15/2012

sahill
sahill šŸ‡®šŸ‡³

4.8

(4)

46 documents

1 / 56

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sorting 7-1
Chapter 7
Sorting
EE 232 Data Structures
Session-05 , Spring-07
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

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