Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B...

18
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

Transcript of Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B...

Page 1: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

Math 130Introduction to Computing

Sorting

Lecture # 17

10/11/04

B Smith:

Save until Week 15?

B Smith:

Save until Week 15?

B Smith:

Skipped Spring 2005?

B Smith:

Skipped Spring 2005?

Page 2: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 2

Overview

• Sorting• Selection Sort

– Animation– Code– Analysis of Selection Sort

• Exchange/Bubble Sort– Animation– Code– Analysis of Bubble Sort

Page 3: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 3

Sorting

• An array a is sorted (ascending order) if:

for all i < j => a[i] a[j]

• Probably the most well-studied algorithmic problem in Computer Science

• There are many algorithms– We will study 2 (others will be studied in M140)– Why not just one?

• Historical• Differ in ease of programming• Differ in performance• What’s “best” sometimes depends on the problem

Page 4: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 4

Sorting in the Real World

• Sorting is one of the most common operations in “data mining”

• Often need to sort enormous sets of data– Don’t fit in memory (RAM)

• These are called external sorts – need to store intermediate results on disks

– Can use multiple machines (parallelism) to make sorting run faster

• Divide and Conquer algorithms (M140)

• When you are faced with the job of sorting:– What sort routines are available?

• The “quicksort” algorithm is commonly available on many platforms

– time is proportional to N*log( N ) on average to sort N items– What does your data look like?

• partially sorted?• reverse order sorted 90% of the time?

Page 5: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 5

What Affects Choice of Algorithm?

• How large is the set of things?• Are you sorting an array or a linked list?• Are you sorting it all at once, or are elements coming in one

at a time (incremental)?• Is the data likely to be (nearly) sorted when you start?• How much time to do you have to write and debug the

code?• How much extra space can you afford, or (related) how

expensive is object allocation/deallocation?– An “in-place” sorting algorithm uses only O(1) space in addition

to the input data structure

• Are there sorting implementations you can use?– Big idea -- don't reinvent the wheel!

Page 6: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 6

Selection Sort

• Selection sort idea: – find smallest element in the array and exchange it with

the element in the first position. – find second smallest element and exchange it with the

element in the second position.

• Do this (n-1) times.

1 5 8 7 9 2 4 2 1 4 4 9 5 7 7 8 8 9

find min

1 5 8 7 9 2 4 input

Page 7: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 7

void selection(int a[], int l, int r) {

for (int i = l; i < r; i++) {

int min = i; for (int j = i+1; j <= r; j++) if (a[j] < a[min]) min = j; swap(&a[i], &a[min]); } }

min to be index of smallest value

Find smallest of entire list

Swap smallest with next unsorted

I = 0I = 1I =2

Selection Sort Code (RS)• Implementation of Selection Sort:void swap(int* a, int* b)

{ int t; t= *a; *a = *b; *b = t; return;}

Page 8: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 8

• For I = 0– for j=1, j <= N, find min – The inner j loop iterates (N-1) times

• For I = 1– for j=2, j <= N, find min– The inner j loop iterates (N-2) times

• For I = 2– for j=3, j <= N, find min– The inner j loop iterates (N-3) times

e.g., N=5, Iterate thru a[1] to a[4], 4

loops

e.g., Iterate thru a[2] to a[4], 3

loops

e.g., Iterate thru a[3] to a[4], 2

loopsFor the last subarray, the number of iterations is just 1

In general, total number of iterations (& comparisons) is:

(N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2

In general, total number of iterations (& comparisons) is:

(N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2

N = number of elements in arraySelection Sort Analysis

Page 9: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 10

Improving Selection Sort

– Can find the min in a balanced tree in O(log n)• May use a tree to improve the insert time for insertion sort or

find-min time for selection sort• Produce O(n log n) sorting algorithms• But if we’re sorting an array, that’s a lot of code• And the constant may be high for object allocation and pointer

manipulation– Can find a min in O(log n) time using a “heap”

• Heap sort like selection sort, but maintains unsorted part in heap• Complicated to write, but can be done in-place

• Selection sort finds minimum by linear search• Can we do better than this?

Page 10: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 11

Exchange (“Bubble”) Sort (RS)

• Bubble Sort Idea– Elements next to each other are swapped so that the list

gets sorted1. Start with comparison of first two elements2. For ascending order, smaller value is placed before larger

value– For descending order,smaller value is placed after larger value

3. Continue until last two items are compared and evaluated for exchange

4. When you can go through the list with no exchanges, the elements are sorted

Page 11: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 13

690 307 32 155 426

690 307 32 155 426307 690 32 155 426307 32 690 155 426307 32 155 690 426307 32 155 426 690

307 32 155 426 690

Bubble Sort pass 1

Page 12: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 14

Bubble Sort - pass 2

307 32 155 426 690

307 32 155 426 69032 307 155 426 69032 155 307 426 690

Page 13: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 15

Bubble Sort

• Note that the largest value always sank to the bottom of list

• The smaller elements slowly rose or “bubbled” up from the bottom

• Each pass always places the largest in the list at the bottom– Hence, no need to do “compare and exchange” for the

final two elements– For each pass, one less compare/exchange operation is

required

Page 14: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 16

Bubble Sort – Analysis

• Similar to Selection Sort– number of comparisons is O(N2).– number of moves depends on initial order of the values

in the list

• Worst case analysis– Occurs when list items are in reverse sorted order

• this is when Selection Sort beats Bubble Sort.– Although both require N(N-1)/2 comparisons…

» Selection Sort needs only N-1 moves» Bubble Sort needs N(N-1)/2 moves

• For random data– Selection Sort generally performs as well as or better

than bubble sort

Page 15: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 17

Bubble Sort Code (RS)

void bubble(int a[], int l, int r)

{ int i, j;

for (i = l; i < r; i++)

for (j = r; j > i; j--)

compswap(a[j-1], a[j]);

}

#define key(A) (A)

#define less(A, B) (key(A) < key(B))

#define compswap(A, B) if(less(B, A)) swap(A, B)

B Smith:

These macros may cause problems. Consider converting to subroutines

B Smith:

These macros may cause problems. Consider converting to subroutines

Page 16: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 18

Summary

Sorting Selection Sort

Animation Code Analysis of Selection Sort

Exchange/Bubble Sort Animation Code Analysis Bubble Sort Analysis

Page 17: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 19

Extra Slides

Page 18: Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

B. Smith 20

Generic Sort Template

#include <stdio.h>

#include <stdlib.h>

typedef int Item;

#define key(A) (A)

#define less(A, B) (key(A) < key(B))

#define exch(A, B) { Item t = A; A = B; B = t; }

#define compexch(A, B) if (less(B, A)) exch(A, B)

void sort(Item a[], int l, int r) //Insertion Sort (R.S.)

{ int i, j;

for (i = l+1; i <= r; i++)

for (j = i; j > l; j--)

compexch(a[j-1], a[j]);

}

// This program takes optional command line arguments as input

// e.g. "Sort01 5 1" will give you 5 random integers and

// "Sort01 99 1" will give you 99 random integers

// and "Sort01" will allow you to input as many integers as you desire

main(int argc, char *argv[])

{ int i, N = atoi(argv[1]), sw = atoi(argv[2]); //srand(time(NULL));

int *a = malloc(N*sizeof(int)); /*dynamic array allocation */

if (sw)

for (i = 0; i < N; i++)

a[i] = 1000*(1.0*rand()/RAND_MAX);

else

for (N = 0; scanf("%d", &a[N]) == 1 ; N++)

; //enter floating pt number or a character to quit entry

sort(a, 0, N-1); // Generic enough to use with any Sort function

for (i = 0; i < N; i++) printf("%3d ", a[i]);

printf("\n");

}