26
Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours: TR 1-2pm or by appointment. Office location: CI2046. Email: simmondsd[@]uncw.edu

Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours:

Embed Size (px)

Citation preview

Sorting

1

Devon M. SimmondsUniversity of North Carolina, Wilmington

TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in 2006. Office hours: TR 1-2pm or by appointment. Office location: CI2046. Email: simmondsd[@]uncw.edu

Objectives

• To introduce basic sort algorithms:– Exchange/bubble sort– Insertion sort– Selection soft

2

3

What is sorting?

Given n elements, arrange them in an increasing or decreasing order by some attribute.

Simplealgorithms:

O(n2)

Insertion sortSelection sortBubble sortShell sort…

Fancieralgorithms:O(n log n)

Heap sortMerge sortQuick sort…

Specializedalgorithms:

O(n)

Bucket sortRadix sort

4

Exchange (Bubble) Sort

• Algorithm (for a list a with n elements)– Repeat until list a is sorted

• for each i from n-1 downto 1– if(a[i] < a[i-1])

» exchange (a[i], a[i-1])

– Result: After the kth pass, the first k elements are sorted.

Example

• 17 21 6 10 16 12 15 4

5

6

Try it out: Bubble sort

• Insert 31, 16, 54, 4, 2, 17, 6

7

Bubble Sort Code

def bubbleSort(self, a): for i in range(len(a)): for j in range(len(a)-1, 0, -1): if(a[j] < a[j-1]): #exchange items temp = a[j-1] a[j-1] = a[j] a[j] = temp

8

Insertion Sort: Idea

• Algorithm (for a list a[0..n] with n elements)– for each i from 1 to n-1

• put the ith element in the correct place among the first i+1 elements

– Result: After the kth pass, the first k elements are sorted.

Example

• 17 21 6 10 16 12 15 4

9

10

Try it out: Insertion sort

• Insert 31, 16, 54, 4, 2, 17, 6

11

Insertion Sort Code

def insertionSort(self, a): for i in range(1, len(a)): #insert a(i) in correct position in a(0) .. a(i) temp = a[i] j = i-1 while (j >= 0 and a[j] > temp): if(a[j] > temp): a[j+1] = a[j] j -= 1 a[j+1] = temp

12

Selection Sort: idea

• Algorithm (for a list a with n elements)– for each i from 1 to n-1

• Find the smallest element, put it in position i-1– Result: After the kth pass, the first k elements are

sorted.

Example

• 17 21 6 10 16 12 15 4

13

14

Try it out: Selection sort

• Insert 31, 16, 54, 4, 2, 17, 6

15

Selection Sort Codedef selectionSort(self, a): for i in range(len(a)-1): #find smallest of items i, i+1, i+2, .., size()-1 and #exchange smallest with item in position i sIndex = i smallest = a[i] #j = i + 1 #while (j < len(a)): for j in range(i+1, len(a)): if(a[j] < smallest): sIndex = j smallest = a[j] #exchange items temp = a[i] a[i] = smallest a[sIndex] = temp

16

QuickSort (Partition Exchange): idea

• Algorithm (for a list a with n elements)– .

QuickSort Example

• 17 21 6 10 16 12 15 4

17

18

Try it out: QuickSort

• Insert 31, 16, 54, 4, 2, 17, 6

19

QuickSort Codedef quickSort(self, array, start, end): left = start right= end

if (right - left < 1): return else:#at least 2 elements to be sorted pivot = array[start] while (right> left): while (array[left] <= pivot and left < right): left += 1 while (array[right] > pivot and right >= left): right -=1 if (right> left): swap(array, left, right) right -= 1 left += 1 #swap array[start] and array[right] temp = array[start] array[start] = array[right] array[right] = temp self.quickSort(array, start, right- 1); self.quickSort(array, right+ 1, end)

20

MergeSort: idea

• Algorithm (for a list a with n elements)

MergeSort Example

• 17 21 6 10 16 12 15 4

21

22

Try it out: MergeSort

• Insert 31, 16, 54, 4, 2, 17, 6

23

MergeSort Codedef mergeSort(self, alist): if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] print("Splitting ",alist, "into", lefthalf, "and", righthalf)

self.mergeSort(lefthalf) self.mergeSort(righthalf) self.merge(lefthalf, righthalf, alist)

def merge(self, lefthalf, righthalf, alist): print("Merging ", lefthalf, "and", righthalf) i=0 j=0 k=0 while i<len(lefthalf) and j<len(righthalf): if lefthalf[i]<righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1

while i<len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1

while j<len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1

Summary

24

25

______________________Devon M. Simmonds

Computer Science Department

University of North Carolina Wilmington

_____________________________________________________________

Qu es ti ons?

Reading from course text:

26

What is sorting?

Given n elements, arrange them in an increasing or decreasing order by some attribute.

Simplealgorithms:

O(n2)

Fancieralgorithms:O(n log n)

Comparisonlower bound:

(n log n)

Specializedalgorithms:

O(n)

Handlinghuge data

sets

Insertion sortSelection sortBubble sortShell sort…

Heap sortMerge sortQuick sort…

Bucket sortRadix sort

External sorting