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

In the subject of the Data Structures, the key concept and the main points, which are very important in the context of the data structures are listed below:Algorithms, Sorting Means, Sorting Rearranges, Ascending, Descending, Operators, Unique Keys, Straight Selection Sort, Already Sorted, Smallest

Typology: Slides

2012/2013

Uploaded on 04/23/2013

saratey
saratey 🇮🇳

4.3

(10)

86 documents

1 / 95

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 10
Sorting
Algorithms
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
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f

Partial preview of the text

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

Chapter 10

Sorting

Algorithms

Divides the array into two parts: already sorted, and not yet sorted.

On each pass,

  • finds the smallest of the unsorted elements, and
  • swaps it into its correct place,
  • thereby increasing the number of sorted elements by one.

Straight Selection Sort

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]

Selection Sort: Pass One

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]

U N S O R T E D

SORTED

Selection Sort: Pass Two

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]
U N S O R T E D

Selection Sort: End Pass Two

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]

U N S O R T E D

SORTED

Selection Sort: End Pass Three

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]
S O R T E D
UNSORTED

Selection Sort: Pass Four

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]
S O R T E D
UNSORTED

Selection Sort:

How many comparisons?

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]

4 compares for values[0]

3 compares for values[1]

2 compares for values[2]

1 compare for values[3]

= 4 + 3 + 2 + 1

For selection sort in general

  • The number of comparisons when the

array contains N elements is

Sum = (N-1) + (N-2) +... + 2 + 1

Arithmetic series:

O(N 2 )

N

N N

i

Sum i

template int MinIndex(ItemType values [ ], int start, int end) // Post: Function value = index of the smallest value // in values [start].. values [end]. { int indexOfMin = start ;

for(int index = start + 1 ; index <= end ; index++) if (values[ index] < values [indexOfMin]) indexOfMin = index ;

return indexOfMin;

}

template void SelectionSort (ItemType values[ ], int numValues )

// Post: Sorts array values[0.. numValues-1 ] // into ascending order by key { int endIndex = numValues - 1; for (int current=0; current<endIndex; current++)

Swap (values[current], values[MinIndex(values, current, endIndex)]);

}

Compares neighboring pairs of array elements,

  • starting with the last array element, and
  • swaps neighbors whenever they are not in correct order.

On each pass, this causes the smallest element to “bubble up” to its correct place in the array.

Bubble Sort

values [ 0 ]

[ 1 ]

[ 2 ]
[ 3 ]
[ 4 ]

Snapshot of BubbleSort