0% found this document useful (0 votes)
32 views

Quick Sort

This document contains code for implementing quicksort. It includes: 1) A C function called colocar() that performs the partition step of quicksort by choosing a pivot element, comparing all other elements to it, and swapping elements on both sides of the pivot into sorted position. 2) A C function called Quicksort() that recursively calls colocar() and itself to sort the entire array passed to it. 3) A Java class with a main() method that demonstrates quicksort by sorting an sample integer array and printing the results before and after sorting. It uses a recursive quick_srt() method to perform the quicksort algorithm.

Uploaded by

sherkarn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Quick Sort

This document contains code for implementing quicksort. It includes: 1) A C function called colocar() that performs the partition step of quicksort by choosing a pivot element, comparing all other elements to it, and swapping elements on both sides of the pivot into sorted position. 2) A C function called Quicksort() that recursively calls colocar() and itself to sort the entire array passed to it. 3) A Java class with a main() method that demonstrates quicksort by sorting an sample integer array and printing the results before and after sorting. It uses a recursive quick_srt() method to perform the quicksort algorithm.

Uploaded by

sherkarn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1:

int colocar(int *v, int b, int t)


{
int i;
int pivote, valor_pivote;
int temp;

pivote = b;
valor_pivote = v[pivote];
for (i=b+1; i<=t; i++){
if (v[i] < valor_pivote){
pivote++;
temp=v[i];
v[i]=v[pivote];
v[pivote]=temp;

}
}
temp=v[b];
v[b]=v[pivote];
v[pivote]=temp;
return pivote;
}

void Quicksort(int* v, int b, int t)


{
int pivote;
if(b < t){
pivote=colocar(v, b, t);
Quicksort(v, b, pivote-1);
Quicksort(v, pivote+1, t);
}
}

2:
public class QuickSort{
  public static void main(String a[]){
    int i;
    int array[] = {12,9,4,99,120,1,3,10,13};

    System.out.println("\n\n       RoseIndia\n\n");
    System.out.println("       Quick Sort\n\n");
    System.out.println("Values Before the sort:\n");
    for(i = 0; i < array.length; i++)
      System.out.print( array[i]+"  ");
    System.out.println();
    quick_srt(array,0,array.length-1);
    System.out.print("Values after the sort:\n");
    for(i = 0; i <array.length; i++)
      System.out.print(array[i]+"  ");
    System.out.println();
    System.out.println("PAUSE");
  }

  public static void quick_srt(int array[],int low, int n){
    int lo = low;
    int hi = n;
    if (lo >= n) {
      return;
    }
    int mid = array[(lo + hi) / 2];
    while (lo < hi) {
      while (lo<hi && array[lo] < mid) {
        lo++;
      }
      while (lo<hi && array[hi] > mid) {
        hi--;
      }
      if (lo < hi) {
        int T = array[lo];
        array[lo] = array[hi];
        array[hi] = T;
      }
    }
    if (hi < lo) {
      int T = hi;
      hi = lo;
      lo = T;
    }
    quick_srt(array, low, lo);
    quick_srt(array, lo == low ? lo+1 : lo, n);
  }
}

You might also like