2621008 - C++ 4

501 views

Published on

Published in: Health & Medicine, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
501
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2621008 - C++ 4

  1. 1. Slide 1
  2. 2. 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.
  3. 3. Slide 3 Problem: How can the rancher easily catalog all of his cattle?
  4. 4. Possible Solution Slide 4 string bessie; string mr_tongue; string miss_fancy; string jumper; … string blondie; Mr. Tongue
  5. 5. Slide 5 New Solution: Arrays const short HERD_SIZE=60; string my_herd[HERD_SIZE];
  6. 6. Slide 6 Array Allocation Syntax Type Name [number of cells];
  7. 7. 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];
  8. 8. 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]
  9. 9. 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]
  10. 10. 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]
  11. 11. 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]
  12. 12. 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]
  13. 13. 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]
  14. 14. 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]
  15. 15. 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]
  16. 16. 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]
  17. 17. Slide 17 Initializing Arrays const int SIZE=5; int array[SIZE]={0}; 0 0 0 0 0 [0] [1] [2] [3] [4]
  18. 18. Slide 18 Initializing Arrays const int SIZE=5; int array[SIZE]={9,1}; 9 1 0 0 0 [0] [1] [2] [3] [4]
  19. 19. Slide 19 Initializing Arrays 5 2 4 [0] [1] [2] int array[]={5,2,4};
  20. 20. 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;
  21. 21. Slide 21 Working With Arrays
  22. 22. 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]
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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
  35. 35. 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
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. 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
  45. 45. 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
  46. 46. 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
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. 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
  51. 51. 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
  52. 52. 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
  53. 53. 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
  54. 54. 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
  55. 55. 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
  56. 56. 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
  57. 57. 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
  58. 58. 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
  59. 59. 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
  60. 60. 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
  61. 61. 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
  62. 62. 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
  63. 63. 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
  64. 64. 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
  65. 65. 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
  66. 66. 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
  67. 67. 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
  68. 68. 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
  69. 69. 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
  70. 70. 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
  71. 71. 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
  72. 72. 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
  73. 73. 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
  74. 74. 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
  75. 75. 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
  76. 76. 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
  77. 77. 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
  78. 78. 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
  79. 79. 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
  80. 80. 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
  81. 81. 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
  82. 82. 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
  83. 83. 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
  84. 84. 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
  85. 85. 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
  86. 86. 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
  87. 87. 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
  88. 88. 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
  89. 89. 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
  90. 90. 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
  91. 91. 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
  92. 92. 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
  93. 93. 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
  94. 94. 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
  95. 95. 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
  96. 96. 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
  97. 97. Slide 97

×