Advertisement
Jokker115

777

Dec 21st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void fill ( int *a, int n){
  7.     for (int i = 0; i < n; i++){
  8.         *(a + i) = 1 + rand () % n;
  9.     }
  10. }
  11.  
  12. void show ( int *a, int n){
  13.     for (int i = 0; i < n; i++){
  14.         cout << *(a + i) << " ";
  15.     }
  16.     cout << endl;
  17. }
  18.  
  19. void bubble_sort ( int *a, int n){
  20.     for (int i = 0; i < (n - 1); i++){
  21.         bool swap = false;
  22.         for (int j = 0; j < (n - i) - 1; j++){
  23.             if ( *(a + j) > *(a + j + 1 )){
  24.                 int b = *(a + j);
  25.                 *(a + j) = *(a + j + 1);
  26.                 *(a + j + 1) = b;
  27.                 swap = true;
  28.             }
  29.         }
  30.         if (!swap)
  31.         break;
  32.     }
  33.  
  34. }
  35.  
  36. bool is_sorted (int *a, int n){
  37.     int index = 0;
  38.     for (int i = 0; i < n; i++){
  39.         if ( a[i] < a[i+1]){
  40.             index++;
  41.         }
  42.     }
  43.     if (index == n){
  44.        cout << "Sorted" << endl;
  45.        return true;
  46.     }
  47.     else {
  48.         cout << "Not sorted" << endl;
  49.         bubble_sort (a, n);
  50.         return false;
  51.     }
  52. }
  53.  
  54. int main ()
  55. {
  56.     int n;
  57.     cout << "Size: ";
  58.     cin >> n;
  59.     int *a = new int [n];
  60.     fill (a, n);
  61.     show (a, n);
  62.     is_sorted (a, n);
  63.     show (a, n);
  64.     delete [] a;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement