Download presentation
Presentation is loading. Please wait.
Published byEustace Green 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 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!
6
Any ideas? 6
7
File into Array Make the array bigger than you will ever need int test_scores[100]; plenty for a class that normally has 20 or 30 students Create and Open the filestream ifstream fin("scores.txt"); then use a counter and loop to read in the file, int k=0; while(fin>>scores[k]) k++; 7
8
File into array, continued afterwards, 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
9
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
10
10 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays
11
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!!
12
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
13
13 Bubble Sort 55229966 55
14
14 Bubble Sort 55229966 > 55 ?
15
15 Bubble Sort 55229966 swap
16
16 Bubble Sort 22559966 55
17
17 Bubble Sort 22559966 > 55 ?
18
18 Bubble Sort 22559966 99
19
19 Bubble Sort 22559966 > 99 ?
20
20 Bubble Sort 22559966 swap
21
21 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
22
22 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; }
23
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; }
24
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)
25
25 Selection Sort 55229966 55 < smallest? F 55 smallest 0123 0 small_pos 0 k data
26
26 Selection Sort 55229966 22 < smallest? T 55 smallest 0 small_pos 0123 0 k data
27
27 Selection Sort 55229966 22 < smallest? T 22 smallest 1 small_pos 0123 0 k data
28
28 Selection Sort 55229966 99 < smallest? F 22 smallest 0123 1 small_pos 0 k data
29
29 Selection Sort 55229966 66 < smallest? F 22 smallest 0123 1 small_pos 0 k data
30
30 Selection Sort—SWAP 55229966 Swap(data[k], data[small_pos]); 22 smallest 0123 1 small_pos 0 k data
31
31 Selection Sort—Repeat 22559966 55 < smallest ? F 55 smallest 0123 1 small_pos 1 k
32
32 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 }
33
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 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]); }
34
34 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort Finding the smallest element in array Multidimensional arrays
35
35 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
36
36 Multidimensional Arrays 0 1 2 3 4 An array
37
37 Multidimensional Arrays 0 1 2 3 An array of arrays 0 1 2 3 4 5 COLUMNS ROWS
38
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};
39
39 Multidimensional Arrays 0 1 2 3 An array of arrays 000000 000000 000000 000000 0 1 2 3 4 5 COLUMNS ROWS matrix
40
40 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;
41
41 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;
42
42 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]; } }
43
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; } }
44
44 That’s a wrap ! What we learned today: Sorting Arrays Bubble Sort Selection Sort Multidimensional arrays
45
45 Go back home proud ! You’re brighter than Ar’ray’ !
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.