Docsity

Naive Sorting Algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Comparison, Slides of Data Structures and Algorithms

Partial preview of the text

Download Naive Sorting Algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Comparison and more Slides Data Structures and Algorithms in PDF only on Docsity!

Survey of Sorting

Naïve sorting algorithms

Bubble sort:

scan for flips, until all are fixed

Etc...

Insertion sort

Sorted subarray

Insertion sort

Algorithm

for

i =

to

n-

do

insert a[i] in the proper place

in

a[0:i-1]

Correctness

•Note: after i steps, the sub-array A[0:i] issorted

Selection sort

Algorithm

for

i =

n-

to

do

Find the

largest entry

in the

in

the subarray A[0:i]

Swap

with A[i]

What is the runtime complexity ofselection sort?

Sorting Comparison

• Discuss the pros and cons of each of the

naïve sorting algorithms

Bucket sort

• In addition to comparing pairs of elements,

we require these additional restrictions:

  • all elements are non-negative integers– all elements are less than a predetermined

maximum value

• Elements are usually keys paired with

other data

Bucket sort

Radix Sort

Radix sort

• If your integers are in a larger range then

do bucket sort on

each digit

• Start by sorting with the

low-order digit

using a

STABLE

bucket sort.

• Then, do the next-lowest,and so on

Radix sort characteristics

• Each sorting step can be performed via

bucket sort, and is thus

O

(N).

• If the numbers are all

b bits

long, then

there are b sorting steps.

• Hence, radix sort is

O

(bN).

What about non-binary?

• Radix sort can be used for decimal

numbers and alphanumeric strings.