Sorting Algorithms: Analysis of Selection, Bubble, and Insertion Sort - Prof. Ajay K. Gupt, Study notes of Computer Science

An explanation of three common sorting algorithms: selection sort, bubble sort, and insertion sort. It discusses the differences between worst-case and average-case analyses, and the number of comparisons and swaps required for each algorithm. The document also includes code snippets for selection sort and bubble sort.

Typology: Study notes

Pre 2010

Uploaded on 07/28/2009

koofers-user-kjx
koofers-user-kjx 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
•1
Sorting Algorithms
Selection, Bubble, Insertion and
Radix Sort
Worst-case
vs.
Average-case Analyses
An algorithm can require different times to
solve different problems of the same size.
Worst-case analysis (find the maximum
number of operations an algorithm can
execute in all situations)
is easier to calculate and is more common
•Average-case (enumerate all possible
situations, find the time of each of the m
possible cases, total and dive by m)
is harder to compute but yields a more realistic
expected behavior
Selection Sort
Divides the array in to two
parts: already sorted , and
not yet sorted.
On each pass, finds t he
smallest of the unsorted
elements, and swap it into
its correct place, th ereby
increasing the number of
sorted elements by on e.
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
36
24
10
6
12
pf3
pf4
pf5
pf8

Partial preview of the text

Download Sorting Algorithms: Analysis of Selection, Bubble, and Insertion Sort - Prof. Ajay K. Gupt and more Study notes Computer Science in PDF only on Docsity!

Sorting Algorithms

Selection, Bubble, Insertion and

Radix Sort

Worst-casevs. Average-case Analyses

  • An algorithm can require different times tosolve different problems of the same size.
  • Worst-case analysis (find the maximumnumber of operations an algorithm can

execute in all situations)– is easier to calculate and is more common

  • Average-case (enumerate all possiblesituations, find the time of each of the m

possible cases, total and dive by m)

  • is harder to compute but yields a more realisticexpected behavior

Selection Sort

Divides the array into two parts: already sorted, andnot yet sorted. On each pass, finds thesmallest of the unsorted elements, and swap it intoits correct place, thereby increasing the number ofsorted elements by one.

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

Selection Sort: Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

Selection Sort: End Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U

NS

OR

TE

D

SORTED

Selection Sort: Pass Two

values [ 0 ] SORTED [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U

NS

OR

T

ED

Selection Sort: Pass Four

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

SO

RT

ED

UNSORTED

Selection Sort: End Pass Four

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

S O R T E D

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+

Notice that...

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

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

2* Sum = N + N +... + N + N

2 * Sum = N * (N-1)

Sum = N * (N-1)/2= .5 N 2 - .5 N

• How many number of swaps?

• O(N)

• Therefore, the complexity is O(N^2 )

Code for Selection Sort

void SelectionSort (int values[], int numValues) //// Post:into ascendingSorts array order values[0 by key.. numValues-1 ] {int endIndex = numValues - 1 ; forSwap (int (values, current=0;current<endIndex;current++) current, MinIndex(values,current,endIndex)); }

Bubble Sort: Pass One

Bubble Sort: Pass Two

Snapshot of BubbleSort

Code for Bubble Sort

void{ BubbleSort(int values[], int numValues) intwhile current (current = 0; < numValues - 1) {BubbleUp(values, current, numValues-1); }current++; }

Bubble Sort code (contd.)

void BubbleUp(intint values[],startIndex, int endIndex) //// Post:order Adjacent have been pairs switched that arebetween out of //// values[startIndex]..values[endIndex]beginning at values[endIndex]. {for (int index = endIndex; indexif (values[index] > startIndex; < index--)values[index-1]) } Swap(values,^ index,^ index-1);

Observations on Bubble Sort

• There can be a large number of

intermediate swaps.

• Can this algorithm be improved?

  • What are the best/worst cases?

• This algorithm is O(N^2 ) in the worst-case