Posted By

trentonknight on 10/22/10


Tagged

sort insertion


Versions (?)

Insertion Sort


 / Published in: C++
 

Just change numbers in array and/or array size to view printout of incremented sort

  1. #include <iostream>
  2. using namespace std;
  3. void insertionSort(int numbers[], int array_size);
  4.  
  5. int main(){
  6.  
  7. int numbers[6] = {47,20,35,26,43,52};
  8. int array_size = 6;
  9.  
  10. insertionSort(numbers,array_size);
  11.  
  12. }
  13.  
  14.  
  15. void insertionSort(int numbers[], int array_size)
  16. {
  17. int z = 0, i, j, index;
  18.  
  19. for (i=1; i < array_size; i++)
  20. {
  21. index = numbers[i];
  22. j = i;
  23. while ((j > 0) && (numbers[j-1] > index))
  24. {
  25. numbers[j] = numbers[j-1];
  26. j = j - 1;
  27. }
  28. numbers[j] = index;
  29. cout << endl;
  30. cout << "Pass: " << i << endl;
  31. for(z = 0; z < array_size; z++){
  32. cout << "[" << numbers[z] << "]";
  33.  
  34. }
  35. }
  36.  
  37. }

Report this snippet  

You need to login to post a comment.