1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort...

45
1 Sorting Arrays Chapter 14

Transcript of 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort...

Page 1: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

1

Sorting ArraysChapter 14

Page 2: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

2

Agenda

Review of Arrays Sorting Arrays

Bubble Sort

Selection Sort

Finding the smallest element in array

Multidimensional arrays

Page 3: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

3

Review Arrays

You know how to declare, initialize, process arrays with loops, and pass them to functions:

float stuff[10]={3, 4, 6, 8, 2, 1, 0};

for (int k=0; k<9; k++)

stuff[k]=stuff[k+1];

Display(stuff, 10);

Page 4: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

4

You can also pass one (or more) individual cells of an array to a function:

int scores[8]={33, 54, 65, 84, 42, 61, 100, 53};

swap(scores[4], scores[1]);

swap(scores[2], scores[7]);

Notice a

Pattern?

void swap(int& x, int& y)

{ // exchanges the values of x , y:

float temp = x;

x = y;

y = temp;

}

Page 5: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

5

A small problem…relates to Lab10 p9-11

How do we read a file into an array?If we don’t know how many lines are in file

How big an array do we need?

How will we keep track of the size of the data set?

For example, look at scores.txt online

Open your notebooks…this is important!

Page 6: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

Any ideas?

6

Page 7: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

File into ArrayMake the array bigger than you will ever needint  test_scores[100];     

plenty for a class that normally has 20 or 30 students

Create and Open the filestreamifstream fin("scores.txt");

then use a counter and  loop to read in the file,  int k=0;

while(fin>>scores[k]) k++;

7

Page 8: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

File into array, continuedafterwards, grab the value of k and store in variable size, that's our array size now:int size=k;

from then on, to display or process the array, loops can use size as the endpoint

for (k=0; k<size; k++)

     cout<<scores[k];

 

8

Page 9: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

What about moving from array to file?

Since you already know the size, it's easy

Open an output filestream, and change cout to the filestream name

9

Page 10: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

10

Agenda

Review of Arrays

Sorting Arrays Bubble Sort

Selection Sort

Finding the smallest element in array

Multidimensional arrays

Page 11: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

11

Sorting Arrays

Computer scientists often need to sort arrays –Why?Because it’s easier to find things in the array when it is sorted Most data looks better displayed in sorted form (phone books, employee records, Lacrosse games) How can we sort an array? What is the algorithm? A: There are several!!

Page 12: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

12

Bubble Sort

Bubble sort is one of the simplest sorting algorithms

It proceeds through a sequence of iterations, each time moving the next largest item into its correct position

On each iteration, it compares each pair of consecutive elements, moving the larger element up

Page 13: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

13

Bubble Sort

55 22 99 66

55

Page 14: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

14

Bubble Sort

55 22 99 66

> 55 ?

Page 15: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

15

Bubble Sort

55 22 99 66

swap

Page 16: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

16

Bubble Sort

22 55 99 66

55

Page 17: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

17

Bubble Sort

22 55 99 66

> 55 ?

Page 18: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

18

Bubble Sort

22 55 99 66

99

Page 19: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

19

Bubble Sort

22 55 99 66

> 99 ?

Page 20: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

20

Bubble Sort

22 55 99 66

swap

Page 21: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

21

Bubble Sort

22 55 66 99

Notice how the data “bubbles up” through the array moving slowly, one bin at a time

After N-1 “Passes” or “Sweeps”, the final array is guaranteed to be sorted in ascending order, no matter what input data

Page 22: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

22

Bubble Sort#include <iostream.h>

void print(float a[], int n); //Prints array avoid sort(float a[], int n);//Sorts array avoid swap(float& , float&);//Swaps a[j] and a[j+1]void main() {float a[] =

{55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7}; print(a,8); sort(a,8); print(a,8); }

void print(float a[], int n) {

for (int i=0; i<n-1; i++)cout<< a[i] << ", ";

cout << a[n-1] << endl; }

Page 23: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

23

Bubble Sort (contd)

void sort(float a[], int n) { for (int i=1; i<n; i++)for ( int j=0; j<n-1; j++)

if(a[j] > a[j+1]) swap(a[j],a[j+1]);}

void swap(float& x, float& y) { float temp;temp=y;y=x;x=temp;

}

Page 24: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

24

Selection Sort

Another way of sorting is the selection sort

The main idea is to keep finding the smallest (and next smallest) items in the array

And move them into correct position (swap)

Page 25: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

25

Selection Sort

55 22 99 66

55 < smallest? F

55

smallest

0 1 2 3

0

small_pos

0

k

data

Page 26: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

26

Selection Sort

55 22 99 66

22 < smallest?

T

55

smallest

0

small_pos

0 1 2 3

0

k

data

Page 27: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

27

Selection Sort

55 22 99 66

22 < smallest?

T

22

smallest

1

small_pos

0 1 2 3

0

k

data

Page 28: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

28

Selection Sort

55 22 99 66

99 < smallest? F

22

smallest

0 1 2 3

1

small_pos

0

k

data

Page 29: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

29

Selection Sort

55 22 99 66

66 < smallest? F

22

smallest

0 1 2 3

1

small_pos

0

k

data

Page 30: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

30

Selection Sort—SWAP

55 22 99 66

Swap(data[k], data[small_pos]);

22

smallest

0 1 2 3

1

small_pos

0

k

data

Page 31: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

31

Selection Sort—Repeat

22 55 99 66

55 < smallest ?

F

55

smallest

0 1 2 3

1

small_pos

1

k

Page 32: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

32

Selection Sort—Finding SmallestAfter (SIZE-1) iterations of the above, array is sortedThe heart of this algorithm is finding the smallest element of the array (and it’s position or index small_pos):

smallest=data[0]; // assume 0th cellsmall_pos=0; // is smallestfor (n=0; n<SIZE; n++) // go thru arrayif (data[n]<smallest) // if smaller{

small_pos=n; //save positionsmallest=data[n]; // and value

}

Page 33: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

33

Selection Sort—the whole function

void Sort(int data[], int size){ int n, k, small_pos, smallest;for (k=0; k<size-1; k++){ smallest=data[k]; // assume kth cell small_pos=k; // is smallest

for (n=k; n<SIZE; n++) if (data[n]<smallest)// if smaller { small_pos=n; //save position

smallest=data[n]; // and value }Swap(data[k], data[small_pos]);

}}

Page 34: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

34

Agenda

Review of Arrays

Sorting Arrays

Bubble Sort

Selection Sort

Finding the smallest element in array

Multidimensional arrays

Page 35: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

35

Multidimensional Arrays

The arrays we have looked at till now have been one-dimensionalThey are linear (or sequential)An array of arrays is called a multidimensional arrayA one-dimensional array of one-dimensional arrays is called a two-dimensional array

Page 36: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

36

Multidimensional Arrays

0

1

2

3

4

An array

Page 37: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

37

Multidimensional Arrays

0

1

2

3

An array of arrays

0 1 2 3 4 5 COLUMNS

ROWS

Page 38: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

38

Multidimensional Array

Simplest way to define a multi-dimensional array is

int matrix[4][6];

This would create a two-dimensional array of type int with 4 rows and 6 columns

int matrix[4][6]={0};

Page 39: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

39

Multidimensional Arrays

0

1

2

3

An array of arrays

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 1 2 3 4 5 COLUMNS

ROWS

matrix

Page 40: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

40

Accessing a 2D Array

0

1

2

3

0 0 0 0 0 44

0 0 0 0 0 0

0 0 0 22 0 0

0 0 0 0 0 0

0 1 2 3 4 5matrix

matrix[2][3]=22;

matrix[0][5]=44;

Page 41: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

41

Processing a 2D Array w/Loop

0

1

2

3

0 0 0 0 0 44

0 0 0 0 0 0

0 0 0 22 0 0

0 1 2 3 4 5

0 1 2 3 4 5matrix

for(k=0; k<6; k++)

matrix[3][k]=k;

Page 42: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

42

2D Array Read/Print Example #include<iostream.h>

void read(int a[][5]); //Read the input into two dimen array a

void print(const int a[][5]);//Print array a

void main()

{ int a[3][5];

read(a);

print(a);

}

void read(int a[][5])

{ cout << "Enter 15 integers, 5 per row:\n";

for (int i=0; i<3; i++)

{

for (int j=0; j<5; j++)

cin >> a[i][j];

}

}

Page 43: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

43

2D Array Example (contd)

void print(const int a[][5])

{ for (int i=0; i<3; i++)

{

cout << "Row " << i << ": ";

for (int j=0; j<5; j++)

cout << " " << a[i][j];

cout << endl;

}

}

Page 44: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

44

That’s a wrap !

What we learned today:Sorting Arrays

Bubble Sort

Selection Sort

Multidimensional arrays

Page 45: 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays  Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional.

45

Go back home proud ! You’re brighter than Ar’ray’ !