Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // This program uses the bubble sort algorithm to sort an
  2. // array in ascending order.
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. // Function prototypes
  7. void sortArray(int [], int);
  8. void showArray(const int [], int);
  9.  
  10. int main()
  11. {
  12. // Array of unsorted values
  13. int values[6] = {7, 2, 3, 8, 9, 1};
  14.  
  15. // Display the values.
  16. cout << "The unsorted values are:\n";
  17. showArray(values, 6);
  18.  
  19. // Sort the values.
  20. sortArray(values, 6);
  21.  
  22. // Display them again.
  23. cout << "The sorted values are:\n";
  24. showArray(values, 6);
  25. return 0;
  26. }
  27.  
  28. //***********************************************************
  29. // Definition of function sortArray *
  30. // This function performs an ascending order bubble sort on *
  31. // array. size is the number of elements in the array. *
  32. //***********************************************************
  33.  
  34. void sortArray(int array[], int size)
  35. {
  36. bool swap;
  37. int temp;
  38.  
  39. for(int x =0; x < (size-1) ; x++)
  40. {
  41. for (int count = 0; count < (size - 1); count++)
  42. {
  43. if (array[count] > array[count + 1])
  44. {
  45. temp = array[count];
  46. array[count] = array[count + 1];
  47. array[count + 1] = temp;
  48.  
  49. }
  50. }
  51. }
  52. }
  53.  
  54. //*************************************************************
  55. // Definition of function showArray. *
  56. // This function displays the contents of array. size is the *
  57. // number of elements. *
  58. //*************************************************************
  59.  
  60. void showArray(const int array[], int size)
  61. {
  62. for (int count = 0; count < size; count++)
  63. cout << array[count] << " ";
  64. cout << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement