Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void swap(int num1, int num2, int* array){
  5.     //swap the left pointer with the right pointer
  6.     int temp = array[num1];
  7.     array[num1] = array[num2];
  8.     array[num2] = temp;
  9.  
  10. }
  11.  
  12. int partition(int left, int right, int pivot, int* array){
  13.     int leftPointer = left - 1;
  14.     int rightPointer = right;
  15.  
  16.     while(1){
  17.        
  18.         while(array[++leftPointer] < pivot){
  19.             //do nothing
  20.         }
  21.  
  22.         while(rightPointer > 0 && array[--rightPointer] > pivot) {
  23.             //do nothing
  24.         }
  25.  
  26.         if(leftPointer >= rightPointer){
  27.             break;
  28.         }
  29.         else {
  30.             swap(leftPointer,rightPointer, array);
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. void quickSort(int left, int right,  int* array){
  37.     if(right-left <= 0)
  38.         return;
  39.  
  40.     else {
  41.         int pivot = array[right];
  42.         int partitionPoint = partition(left,right,pivot, array);
  43.         quickSort(left, partitionPoint - 1, array);
  44.         quickSort(partitionPoint+1,right, array);
  45.  
  46.     }
  47. }
  48.  
  49. void matrixMath(){
  50.     int size, min, max, avg, med, uniq, non_uniq;
  51.     int* array; //declare a pointer variable to point to the heap space
  52.     int* sortedArray;
  53.     printf("enter size: \n");
  54.     scanf("%d", &size);
  55.  
  56.     //call malloc to allocate to get the right space for the array in bytes
  57.     array = (int *)malloc(sizeof(int)*size);
  58.     sortedArray = (int *)malloc(sizeof(int)*size);
  59.     printf("enter min integer: \n");
  60.     scanf("%d", &min);
  61.     printf("enter max integer: \n");
  62.     scanf("%d", &max);
  63.  
  64.     for(int i = 0; i < size; i++){
  65.         array[i] = rand() % (max - min + 1) + min;
  66.         printf("%d ", array[i]);
  67.     }
  68.  
  69.     printf("\n");
  70.     //sort using a QuickSort algorithm
  71.     quickSort(0, size-1, array );
  72.  
  73.     for(int i = 0; i < size; i++){
  74.         printf("%d ", array[i]);
  75.     }
  76.  
  77.  
  78. }
  79.  
  80. int main(void) {
  81.     matrixMath();
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement