Wednesday, 28 June 2017

Quick Sort Algorithm

 


void main()
{
    int arr[] = {23,33,1,3,9,4,2,1,30,5,13,43,43,3,1,5};
     int i,n;
     n = sizeof(arr)/sizeof(arr[0]);

    quick_sort(arr,0,n-1);
    printf("\nThe Sorted Array is:\n\n");

     for(i=0 ; i          printf(" %4d",arr[i]);
      }
}



void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

 int partition(int *arr, int low, int high){
        int pivot =  arr[high];  

        int i = low-1;
       int tmp;
       for (int j = low; j<=high-1; j++){
           if (arr[j]<= pivot) {
           i++;
           swap(arr[I], arr[j])'
          }
     }

   swap(arr[high], arr[I+1]);
  return(I+1);
  }

       

 
void quicksort(int a[], int low, int high)
{
 int j;

 if(low < high)
 {
 j = partition(a, low, high);
 quicksort(a, low, j - 1);
 quicksort(a, j + 1, high);
 }
}

No comments:

Post a Comment