1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort...
-
Upload
eustace-green -
Category
Documents
-
view
233 -
download
1
Transcript of 1 Sorting Arrays Chapter 14. 2 Agenda Review of Arrays Sorting Arrays Bubble Sort Selection Sort...
1
Sorting ArraysChapter 14
2
Agenda
Review of Arrays Sorting Arrays
Bubble Sort
Selection Sort
Finding the smallest element in array
Multidimensional arrays
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
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
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!
Any ideas?
6
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
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
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
Agenda
Review of Arrays
Sorting Arrays Bubble Sort
Selection Sort
Finding the smallest element in array
Multidimensional arrays
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
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
Bubble Sort
55 22 99 66
55
14
Bubble Sort
55 22 99 66
> 55 ?
15
Bubble Sort
55 22 99 66
swap
16
Bubble Sort
22 55 99 66
55
17
Bubble Sort
22 55 99 66
> 55 ?
18
Bubble Sort
22 55 99 66
99
19
Bubble Sort
22 55 99 66
> 99 ?
20
Bubble Sort
22 55 99 66
swap
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
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; }
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
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
Selection Sort
55 22 99 66
55 < smallest? F
55
smallest
0 1 2 3
0
small_pos
0
k
data
26
Selection Sort
55 22 99 66
22 < smallest?
T
55
smallest
0
small_pos
0 1 2 3
0
k
data
27
Selection Sort
55 22 99 66
22 < smallest?
T
22
smallest
1
small_pos
0 1 2 3
0
k
data
28
Selection Sort
55 22 99 66
99 < smallest? F
22
smallest
0 1 2 3
1
small_pos
0
k
data
29
Selection Sort
55 22 99 66
66 < smallest? F
22
smallest
0 1 2 3
1
small_pos
0
k
data
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
31
Selection Sort—Repeat
22 55 99 66
55 < smallest ?
F
55
smallest
0 1 2 3
1
small_pos
1
k
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
}
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]);
}}
34
Agenda
Review of Arrays
Sorting Arrays
Bubble Sort
Selection Sort
Finding the smallest element in array
Multidimensional arrays
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
36
Multidimensional Arrays
0
1
2
3
4
An array
37
Multidimensional Arrays
0
1
2
3
An array of arrays
0 1 2 3 4 5 COLUMNS
ROWS
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
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
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;
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;
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];
}
}
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
That’s a wrap !
What we learned today:Sorting Arrays
Bubble Sort
Selection Sort
Multidimensional arrays
45
Go back home proud ! You’re brighter than Ar’ray’ !