Advertisement
totobac

Untitled

Jan 17th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. const int MAX_SIZE = 1000;
  5.  
  6. bool isSorted(int array[], int n, int i)
  7. {
  8.     if (i == n - 2)
  9.     {
  10.         if (array[i] > array[i+1])
  11.         {
  12.             return false;
  13.         }
  14.     }
  15.     else
  16.     {
  17.         if (array[i] > array[i + 1])
  18.         {
  19.             return false;
  20.         }
  21.         isSorted(array, n, i + 1);
  22.     }
  23.     return true;
  24. }
  25.  
  26. void printArray(int array[], int n, int i)
  27. {
  28.     if (i == n - 1)
  29.     {
  30.         cout << array[i];
  31.     }
  32.     else
  33.     {
  34.         cout << array[i];
  35.         isSorted(array, n, i + 1);
  36.     }
  37. }
  38. void loopThroughArray(int array[], int newArray[], int n, int i, int counter)
  39. {
  40.     if (counter == i-1)
  41.         newArray[i] = array[i];
  42.  
  43.     if (i == n - 1)
  44.     {
  45.         newArray[i] = array[i];
  46.     }
  47.     else
  48.     {
  49.         if (newArray[i] > newArray[i - 1] )
  50.             counter++;
  51.         loopThroughArray(array, newArray, n, i + 1, counter);
  52.     }
  53. }
  54. void insertInSorted(int array[], int newArray[],int n, int i)
  55. {
  56.    
  57.     if (i == 0)
  58.         newArray[i] = array[i];
  59.     if (i == 1)
  60.     {
  61.         if (newArray[i] > newArray[i-1])
  62.         {
  63.             newArray[i] = array[i];
  64.         }
  65.         else {
  66.             int temp;
  67.             temp = newArray[i - 1];
  68.             newArray[i - 1] = newArray[i];
  69.             newArray[i] = temp;
  70.         }
  71.     }
  72.        
  73.     else if (i == n )
  74.         return;
  75.     else
  76.     {
  77.         loopThroughArray(array, newArray, n, 0,0);
  78.     }
  79.  
  80.     insertInSorted(array, newArray, n, i + 1);
  81.  }
  82.  
  83. int main()
  84. {
  85.     int n;
  86.     int array[MAX_SIZE];
  87.     int newArray[MAX_SIZE];
  88.     cout << "Enter your N: ";
  89.     cin >> n;
  90.     for (size_t i = 0; i < n; i++)
  91.     {
  92.         cin >> array[i];
  93.     }
  94.     insertInSorted(array, newArray, n, 0);
  95.     isSorted(newArray, n, 0);
  96.     printArray(array, n, 0);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement