Download presentation
Presentation is loading. Please wait.
Published byFerdinand Stone Modified over 9 years ago
1
1 Sorting Arrays Chapter 14
2
2 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays
3
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);
4
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; }
5
5 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!
6
6 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays
7
7 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!!
8
8 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
9
9 Bubble Sort 55229966 55
10
10 Bubble Sort 55229966 > 55 ?
11
11 Bubble Sort 55229966 swap
12
12 Bubble Sort 22559966 55
13
13 Bubble Sort 22559966 > 55 ?
14
14 Bubble Sort 22559966 99
15
15 Bubble Sort 22559966 > 99 ?
16
16 Bubble Sort 22559966 swap
17
17 Bubble Sort 22556699 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
18
18 Bubble Sort #include 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; }
19
19 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; }
20
20 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)
21
21 Selection Sort 55229966 55 < smallest? F 55 smallest 0123 0 small_pos 0 k data
22
22 Selection Sort 55229966 22 < smallest? T 55 smallest 0 small_pos 0123 0 k data
23
23 Selection Sort 55229966 22 < smallest? T 22 smallest 1 small_pos 0123 0 k data
24
24 Selection Sort 55229966 99 < smallest? F 22 smallest 0123 1 small_pos 0 k data
25
25 Selection Sort 55229966 66 < smallest? F 22 smallest 0123 1 small_pos 0 k data
26
26 Selection Sort—SWAP 55229966 Swap(data[k], data[small_pos]); 22 smallest 0123 1 small_pos 0 k data
27
27 Selection Sort—Repeat 22559966 55 < smallest ? F 55 smallest 0123 1 small_pos 1 k
28
28 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 0 th 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 }
29
29 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 k th 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]); }
30
30 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays
31
31 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 one- dimensional arrays is called a two- dimensional array
32
32 Multidimensional Arrays 0 1 2 3 4 An array
33
33 Multidimensional Arrays 0 1 2 3 An array of arrays 0 1 2 3 4 5 COLUMNS ROWS
34
34 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};
35
35 Multidimensional Arrays 0 1 2 3 An array of arrays 000000 000000 000000 000000 0 1 2 3 4 5 COLUMNS ROWS matrix
36
36 Accessing a 2D Array 0 1 2 3 0000044 000000 0002200 000000 0 1 2 3 4 5 matrix matrix[2][3]=22; matrix[0][5]=44;
37
37 Processing a 2D Array w/Loop 0 1 2 3 0000044 000000 0002200 012345 0 1 2 3 4 5 matrix for(k=0; k<6; k++) matrix[3][k]=k;
38
38 2D Array Read/Print Example #include 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]; } }
39
39 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; } }
40
40 That’s a wrap ! What we learned today: Sorting Arrays Bubble Sort Selection Sort Multidimensional arrays
41
41 Go back home proud ! You’re brighter than Ar’ray’ !
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.