• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
2621008 -  C++ 4
 

2621008 - C++ 4

on

  • 182 views

 

Statistics

Views

Total Views
182
Views on SlideShare
182
Embed Views
0

Actions

Likes
0
Downloads
16
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    2621008 -  C++ 4 2621008 - C++ 4 Presentation Transcript

    • Slide 1
    • Slide 2 Arrays An array is an aggregate data type that lets you  access multiple variables through a single name  by use of an index. In C++, all of these variables  must have the same type.
    • Slide 3 Problem: How can the rancher easily catalog all of his cattle?
    • Possible Solution Slide 4 string bessie; string mr_tongue; string miss_fancy; string jumper; … string blondie; Mr. Tongue
    • Slide 5 New Solution: Arrays const short HERD_SIZE=60; string my_herd[HERD_SIZE];
    • Slide 6 Array Allocation Syntax Type Name [number of cells];
    • Slide 7 Array Allocation Syntax // examples int student_numbers[50]; float student_grades[50]; string student_names[50]; const short NUM_FRIENDS = 2; string my_friends_names[NUM_FRIENDS];
    • Slide 8 Arrays Visualized const int SAMPLE_SIZE=340; float deflection_readings [SAMPLE_SIZE]; 3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 9 Accessing Elements deflection [0]=45.1; deflection [4]=12.1; cout << deflection_readings [6] << endl; 3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 3.56 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 11 ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 45.1 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 12 ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 45.1 3.44 4.03 3.96 3.77 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 13 ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 14 ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 15 ction_readings[0]=45.1; ction_readings[4]=12.1; << deflection_readings[6] << endl; 45.1 3.44 4.03 3.96 12.1 3.49 3.92 … 4.01 3.83 3.21 [0] [1] [2] [3] [4] [5] [6] … [337] [338] [339]
    • Slide 16 Initializing Arrays const int SIZE=5; int array[SIZE]={7,2,4,5,1}; 7 2 4 5 1 [0] [1] [2] [3] [4]
    • Slide 17 Initializing Arrays const int SIZE=5; int array[SIZE]={0}; 0 0 0 0 0 [0] [1] [2] [3] [4]
    • Slide 18 Initializing Arrays const int SIZE=5; int array[SIZE]={9,1}; 9 1 0 0 0 [0] [1] [2] [3] [4]
    • Slide 19 Initializing Arrays 5 2 4 [0] [1] [2] int array[]={5,2,4};
    • Slide 20 Things You Can NOT Do Output an entire array like int, float, or strings int array[10]; cout << array << endl; Read in to an entire array int array[10]; cin >> array;
    • Slide 21 Working With Arrays
    • Slide 22 Traversals hort SIZE=5; ges[SIZE]={0}; um=0; verage=0; rt i=0; i<SIZE; i++) t << “Person “ << i+1 << “, enter your age: “; >> ages[i]; 0 0 0 0 0 [0] [1] [2] [3] [4]
    • Slide 23 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 0 0 0 0 0 [0] [1] [2] [3] [4] i=0
    • Slide 24 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 0 0 0 0 [0] [1] [2] [3] [4] i=0
    • Slide 25 Traversals hort SIZE=5; ges[SIZE]={0}; um=0; verage=0; rt i=0; i<SIZE; i++) t << “Person “ << i+1 << “, enter your age: “; >> ages[i]; 12 0 0 0 0 [0] [1] [2] [3] [4] i=1
    • Slide 26 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; hort i=0; i<SIZE; i++) out << “Person “ << i+1 << “, enter your age: “; in >> ages[i]; 12 15 0 0 0 [0] [1] [2] [3] [4] i=1
    • Slide 27 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 15 0 0 0 [0] [1] [2] [3] [4] i=2
    • Slide 28 Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } 12 15 31 0 0 [0] [1] [2] [3] [4] i=2
    • Slide 29 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 15 31 0 0 [0] [1] [2] [3] [4] i=3
    • Slide 30 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 15 31 18 0 [0] [1] [2] [3] [4] i=3
    • Slide 31 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 15 31 18 0 [0] [1] [2] [3] [4] i=4
    • Slide 32 Traversals short SIZE=5; ages[SIZE]={0}; sum=0; average=0; ort i=0; i<SIZE; i++) ut << “Person “ << i+1 << “, enter your age: “; n >> ages[i]; 12 15 31 18 14 [0] [1] [2] [3] [4] i=4
    • Slide 33 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=0, sum=0
    • Slide 34 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=0, sum=12
    • Slide 35 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=1, sum=12
    • Slide 36 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=1, sum=27
    • Slide 37 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=2, sum=27
    • Slide 38 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=2, sum=58
    • Slide 39 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=3, sum=58
    • Slide 40 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=3, sum=76
    • Slide 41 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=4, sum=76
    • Slide 42 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=4, sum=90
    • Slide 43 Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; 12 15 31 18 14 [0] [1] [2] [3] [4] i=4, sum=90 average=18
    • Slide 44 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=12
    • Slide 45 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=12 i=1
    • Slide 46 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=12 i=1
    • Slide 47 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=15 i=1
    • Slide 48 Traversals rt max=ages[0]; (short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; t << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=15 i=2
    • Slide 49 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=15 i=2
    • Slide 50 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31 i=2
    • Slide 51 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31 i=3
    • Slide 52 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31 i=3
    • Slide 53 Traversals short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31 i=4
    • Slide 53 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31 i=4
    • Slide 55 Traversals ort max=ages[0]; r(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; ut << “the max age in the array is “ << max; 12 15 31 18 14 [0] [1] [2] [3] [4] max=31
    • Slide 56 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1
    • Slide 57 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j=0
    • Slide 58 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j j+1 j=0
    • Slide 59 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j=1
    • Slide 60 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j j+1 j=1
    • Slide 61 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j=2
    • Slide 62 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j j+1 j=2
    • Slide 63 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 31 18 14 [0] [1] [2] [3] [4] i=1 j j+1 j=2 swap=31
    • Slide 64 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 18 14 [0] [1] [2] [3] [4] i=1 j j+1 j=2 swap=31
    • Slide 65 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 31 14 [0] [1] [2] [3] [4] i=1 j j+1 j=2 swap=31
    • Slide 66 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 31 14 [0] [1] [2] [3] [4] i=1 j=3
    • Slide 67 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 31 14 [0] [1] [2] [3] [4] i=1 j j+1 j=3
    • Slide 68 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 31 14 [0] [1] [2] [3] [4] i=1 j j+1 j=3 swap=31
    • Slide 69 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 14 [0] [1] [2] [3] [4] i=1 j j+1 j=3 swap=31
    • Slide 70 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=1 j j+1 j=3 swap=31
    • Slide 71 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=1 j=4
    • Slide 72 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2
    • Slide 73 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j=0
    • Slide 74 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j j+1 j=0
    • Slide 75 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j=1
    • Slide 76 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j j+1 j=1
    • Slide 77 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j=2
    • Slide 78 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j j+1 j=2
    • Slide 79 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 18 14 31 [0] [1] [2] [3] [4] i=2 j j+1 j=2 swap=18
    • Slide 80 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 14 31 [0] [1] [2] [3] [4] i=2 j j+1 j=2 swap=18
    • Slide 81 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=2 j j+1 j=2 swap=18
    • Slide 82 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=2 j=3
    • Slide 83 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3
    • Slide 84 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3 j=0
    • Slide 85 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3 j j+1 j=0
    • Slide 86 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3 j=1
    • Slide 87 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3 j j+1 j=1
    • Slide 88 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 15 14 18 31 [0] [1] [2] [3] [4] i=3 j j+1 j=1 swap=15
    • Slide 89 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 14 18 31 [0] [1] [2] [3] [4] i=3 j j+1 j=1 swap=15
    • Slide 90 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=3 j j+1 j=1 swap=15
    • Slide 91 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=3 j=2
    • Slide 92 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=4
    • Slide 93 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=4 j j+1 j=0
    • Slide 94 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=4 j=1
    • Slide 95 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=5
    • Slide 96 Traversals for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } 12 14 15 18 31 [0] [1] [2] [3] [4] i=5
    • Slide 97