Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void swap(int num1, int num2, int* array){
- //swap the left pointer with the right pointer
- int temp = array[num1];
- array[num1] = array[num2];
- array[num2] = temp;
- }
- int partition(int left, int right, int pivot, int* array){
- int leftPointer = left - 1;
- int rightPointer = right;
- while(1){
- while(array[++leftPointer] < pivot){
- //do nothing
- }
- while(rightPointer > 0 && array[--rightPointer] > pivot) {
- //do nothing
- }
- if(leftPointer >= rightPointer){
- break;
- }
- else {
- swap(leftPointer,rightPointer, array);
- }
- }
- }
- void quickSort(int left, int right, int* array){
- if(right-left <= 0)
- return;
- else {
- int pivot = array[right];
- int partitionPoint = partition(left,right,pivot, array);
- quickSort(left, partitionPoint - 1, array);
- quickSort(partitionPoint+1,right, array);
- }
- }
- void matrixMath(){
- int size, min, max, avg, med, uniq, non_uniq;
- int* array; //declare a pointer variable to point to the heap space
- int* sortedArray;
- printf("enter size: \n");
- scanf("%d", &size);
- //call malloc to allocate to get the right space for the array in bytes
- array = (int *)malloc(sizeof(int)*size);
- sortedArray = (int *)malloc(sizeof(int)*size);
- printf("enter min integer: \n");
- scanf("%d", &min);
- printf("enter max integer: \n");
- scanf("%d", &max);
- for(int i = 0; i < size; i++){
- array[i] = rand() % (max - min + 1) + min;
- printf("%d ", array[i]);
- }
- printf("\n");
- //sort using a QuickSort algorithm
- quickSort(0, size-1, array );
- for(int i = 0; i < size; i++){
- printf("%d ", array[i]);
- }
- }
- int main(void) {
- matrixMath();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement