


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































13_arrays_sorting.ppt
dataType[ ] arrayName = new dataType[arraySize];
Position Address Index Value
1 1010 [0] 0
2 1014 [1] 0
3 1018 [2] 0
4 1022 [3] 0
5 1026 [4] 0
6 1030 [5] 0
7 1034 [6] 0
… … … …
20 1086 [19] 0
//Loads Array with scores saved in a data file. private void btnLoadArray_Click( ) { FileStream studentFile = new FileStream("cs12ex.txt", FileMode.Open); StreamReader studentStreamReader = new StreamReader(studentFile); int i = 0; // subscript initialized to zero while (studentStreamReader.Peek() != -1) { if (i < cintTestScores.Length) { cintTestScores[i] = int.Parse(studentStreamReader.ReadLine( )); i ++; //Increment subscript by one } else { MessageBox.Show (“Array Size Exceeded”); break; //Get of out of loop; Array is full. } } cintNumberOfStudents = i; // Save how many students were loaded studentFile.Close( ); //Close file }
cs12ex.txt 50 40 100 30 10 20
const int intARRAY_SIZE = 20; int[ ] cintTestScores = new int[intArraySize]; int cintNumberOfStudents;
Address Subscript Value 1010 [0] 0 1014 [1] 0 1018 [2] 0 1022 [3] 0 1026 [4] 0 1030 [5] 0 1034 [6] 0 … … … 1086 [19] 0
Subscript Value [0] 50 [1] 40 [2] 100 [3] 30 [4] 10 [5] 20 [6] 0 … … [19] 0
Move the value in the 1st position to holdValue = 50.
Find the lowest value and move it to the 1st position.
Move holdValue to the position that the lowest value was found, swap values.
Subscript Value [0] 10 [1] 40 [2] 100 [3] 30 [4] 50 [5] 20 [6] 0 … … [19] 0
Move the value in the 2 nd^ position to holdValue = 40.
Find the next lowest value and move it to the 2nd position.
Move holdValue to the position that the lowest value was found.
Subscript Value [0] 10 [1] 20 [2] 30 [3] 100 [4] 50 [5] 40 [6] 0 … … [19] 0
Move the value in the 4th position to holdValue = 100.
Find the next lowest value and move it to the 4th position.
Move holdValue to the position that the lowest value was found.
Subscript Value [0] 10 [1] 20 [2] 30 [3] 40 [4] 50 [5] 100 [6] 0 … … [19] 0
Move the value in the 5th position to holdValue = 50.
A lower value than 50 will not be found, but the comparison must still be made.