Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void sortList(int [], int); // function prototype
  5.  
  6. int main()
  7. {
  8. const int MAX = 4;
  9.  
  10. int list[MAX] = {22,5,67,98};
  11. int i, moves;
  12.  
  13. moves = sortList(list, MAX);
  14.  
  15. cout << "The sorted list, in ascending order, is:\n";
  16. for (i = 0; i < MAX; ++i)
  17. cout << " " <<list[i];
  18.  
  19. cout << endl << moves << " moves were made to sort this list\n";
  20.  
  21. return 0;
  22. }
  23.  
  24. void sortList(int list[], int max)
  25. {
  26. int i, j, temp, moves = 0;
  27.  
  28. for ( i = 0; i < (max - 1); i++)
  29. {
  30. for(j = 1; j < max; j++)
  31. {
  32. if (list[j] < list[j-1])
  33. {
  34. temp = list[j];
  35. list[j] = list[j-1];
  36. list[j-1] = temp;
  37. moves++;
  38. }
  39. }
  40. }
  41.  
  42. return moves;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement