Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. bool isSorted(int *a, int n)
  6. {
  7.     bool sorted = true;
  8.     int count = 0;
  9.     while (count < n - 1)
  10.     {
  11.         if (a[count] > a[count + 1])
  12.             sorted = false;
  13.         count++;
  14.     }
  15.     return sorted;
  16. }
  17.  
  18. void insertionSort(int *a, int n)
  19. {
  20.     int temp, j;
  21.     for (int i = 0; i < n; i++)
  22.     {
  23.         j = i;
  24.         //While J is greater than 0 AND an element is less than the one before it
  25.         while (j > 0 && a[j] < a[j - 1])
  26.         {
  27.             //If true swap and decrease the value of j to check again
  28.             temp = a[j];
  29.             a[j] = a[j - 1];
  30.             a[j - 1] = temp;
  31.             //Decreasing the value of j is used
  32.             j--;
  33.         }
  34.     }
  35. }
  36.  
  37. void quickSort(int *a, int left, int right)
  38. {
  39.     /* partitioning */
  40.     //Half way between the ranges
  41.     int mid = ((left + right) / 2);
  42.     //Lowest value
  43.     int low = left;
  44.     //Highest value
  45.     int high = right;
  46.     //Pivot point is the middle value in the array
  47.     int pivot = a[mid];
  48.  
  49.     int tmp = -1;
  50.     //While lowest value is less than or equal to highest value
  51.     while (low <= high)
  52.     {
  53.         //while the first value in the array is less than the pivot point, check the next element
  54.         while (a[low] < pivot){ low++; };
  55.         //while the highest value is higher than the pivot point, move to the next highest
  56.         while (a[high] > pivot){ high--; };
  57.         //If the lowest element is less than or qual to the highest, swap the values
  58.         if (low <= high)
  59.         {
  60.             //swap
  61.             tmp = a[low];
  62.             a[low] = a[high];
  63.             a[high] = tmp;
  64.             low++; high--;
  65.         }
  66.     }
  67.     /* end partitioning */
  68.     if (left < high) quickSort(a, left, high);
  69.     if (low < right) quickSort(a, low, right);
  70.  
  71. }
  72. void merging(int *a, int first, int mid, int last)
  73. {
  74.     int l1, l2, i;
  75.     int b[500]; // Length of array
  76.  
  77.     for (l1 = first, l2 = mid + 1, i = first; l1 <= mid && l2 <= last; i++)
  78.     {
  79.         if (a[l1] <= a[l2]) { b[i] = a[l1++]; }
  80.         else { b[i] = a[l2++]; }
  81.     }
  82.     while (l1 <= mid) { b[i++] = a[l1++]; }
  83.     while (l2 <= last) { b[i++] = a[l2++]; }
  84.  
  85.     for (i = first; i <= last; i++)
  86.         a[i] = b[i];
  87.  
  88. }
  89.  
  90. void mergeSort(int *a, int first, int last)
  91. {
  92.     int mid = ((first + last) / 2);
  93.  
  94.     if (first < last)
  95.     {
  96.  
  97.         mergeSort(a, first, mid);
  98.         mergeSort(a, mid + 1, last);
  99.         merging(a, first, mid, last);
  100.     }
  101. }
  102.  
  103. int main()
  104. {
  105.     //Out of order test
  106.     int a[] = { 15, 20, 23, 18, 30 };
  107.     if (isSorted(a, 5) == true)
  108.         cout << "A: The array is sorted" << endl;
  109.     else
  110.         cout << "A: The array is not sorted" << endl;
  111.     //In order test
  112.     int b[] = { 15, 20, 23, 28, 30 };
  113.     if (isSorted(b, 5) == true)
  114.         cout << "B: The array is sorted" << endl;
  115.     else
  116.         cout << "B: The array is not sorted" << endl;
  117.     int c[] = { 5, 4, 7, 2, 6, 1, 3 };
  118.     if (isSorted(c, 7) == true)
  119.         cout << "C: The array is sorted" << endl;
  120.     else
  121.         cout << "C: The array is not sorted" << endl;
  122.     insertionSort(c, 7);
  123.     if (isSorted(c, 7) == true)
  124.         cout << "C: The array is sorted" << endl;
  125.     else
  126.         cout << "C: The array is not sorted" << endl;
  127.  
  128.     int d[] = { 5, 4, 7, 2, 6, 1, 3 };
  129.     if (isSorted(d, 7) == true)
  130.         cout << "D: The array is sorted" << endl;
  131.     else
  132.         cout << "D: The array is not sorted" << endl;
  133.     quickSort(d, 0,6);
  134.     if (isSorted(d, 7) == true)
  135.         cout << "D: The array is sorted" << endl;
  136.     else
  137.         cout << "D: The array is not sorted" << endl;
  138.  
  139.     int e[] = { 5, 4, 7, 2, 6, 1, 3 };
  140.     if (isSorted(e, 7) == true)
  141.         cout << "E: The array is sorted" << endl;
  142.     else
  143.         cout << "E: The array is not sorted" << endl;
  144.     quickSort(e, 0, 6);
  145.     if (isSorted(e, 7) == true)
  146.         cout << "E: The array is sorted" << endl;
  147.     else
  148.         cout << "E: The array is not sorted" << endl;
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement