Sorting Arrays Chapter 14 1 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays 2 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); 3 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; } 4 A small problem…relates to Lab13 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! 5 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays 6 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!! 7 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 8 Bubble Sort 55 22 99 66 55 9 Bubble Sort 55 22 99 66 > 55 ? 10 Bubble Sort 55 22 99 66 swap 11 Bubble Sort 22 55 99 66 55 12 Bubble Sort 22 55 99 66 > 55 ? 13 Bubble Sort 22 55 99 66 99 14 Bubble Sort 22 55 99 66 > 99 ? 15 Bubble Sort 22 55 99 66 swap 16 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 17 Bubble Sort #include <iostream.h> void print(float a[], int n); //Prints array a void sort(float a[], int n);//Sorts array a void 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; } 18 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; } 19 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) 20 Selection Sort data 0 1 2 3 55 22 99 66 smallest small_pos 55 0 k 0 55 < smallest? F 21 Selection Sort data 0 1 2 3 55 22 99 66 smallest small_pos 55 0 k 0 22 < smallest? T 22 Selection Sort data 0 1 2 3 55 22 99 66 smallest small_pos 22 1 k 0 22 < smallest? T 23 Selection Sort data 0 1 2 3 55 22 99 66 smallest small_pos 22 1 k 0 99 < smallest? F 24 Selection Sort data 0 1 2 3 55 22 99 66 smallest small_pos 22 1 k 0 66 < smallest? F 25 Selection Sort—SWAP data smallest 22 0 1 2 3 55 22 99 66 small_pos 1 k 0 Swap(data[k], data[small_pos]); 26 Selection Sort—Repeat 0 1 2 3 22 55 99 66 smallest small_pos 55 1 k 1 55 < smallest ? F 27 Selection Sort—Finding Smallest After (SIZE-1) iterations of the above, array is sorted The 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 cell small_pos=0; // is smallest for (n=0; n<SIZE; n++) // go thru array if (data[n]<smallest) // if smaller { small_pos=n; //save position smallest=data[n]; // and value } 28 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]); } } 29 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays 30 Multidimensional Arrays The arrays we have looked at till now have been one-dimensional They are linear (or sequential) An array of arrays is called a multidimensional array A one-dimensional array of onedimensional arrays is called a twodimensional array 31 Multidimensional Arrays 0 1 2 3 4 An array 32 Multidimensional Arrays 0 1 2 3 4 5 COLUMNS 0 1 2 3 ROWS An array of arrays 33 Multidimensional Array Simplest way to define a multidimensional 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}; 34 Multidimensional Arrays 1 2 0 0 0 0 0 2 0 3 0 matrix 0 1 ROWS 0 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 COLUMNS An array of arrays 35 Accessing a 2D Array matrix[2][3]=22; matrix[0][5]=44; 1 2 0 0 0 0 0 2 0 3 0 matrix 0 1 0 3 4 5 0 0 44 0 0 0 0 0 0 22 0 0 0 0 0 0 0 36 Processing a 2D Array w/Loop for(k=0; k<6; k++) matrix[3][k]=k; 1 2 0 0 0 0 0 2 0 3 0 matrix 0 1 0 3 4 5 0 0 44 0 0 0 0 0 0 22 0 0 1 2 3 5 4 37 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]; } } 38 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; } } 39 That’s a wrap ! What we learned today: Sorting Arrays Bubble Sort Selection Sort Multidimensional arrays 40 Go back home proud ! You’re brighter than Ar’ray’ ! 41