Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by nerh ( 16 years ago )
#include <stdio.h>
#include <stdlib.h>

int split(int *array, int b_pos, int e_pos);
void quick(int *array, int b_pos, int e_pos);

int main(){
   int array[100];
   int i;
   for(i = 0; i<100; i++){
      array[i]=rand()0;
   }
   quick(array,0,100);
   for(i = 0; i<100; i++)
      printf("%d ",array[i]);
   printf("\n");
   return 0;
}

int split(int *array, int b_pos, int e_pos){
   int l,r,m,temp,piv_val;
   l = b_pos;
   r = e_pos-1;
   m = b_pos + rand()%(e_pos-b_pos);
   while(l!=r){
      while(array[l]<array[m] && l<m)
  l++;
      while(array[r]>=array[m] && r>m)
  r--;
      temp = array[l];
      array[l] = array[r];
      array[r] = temp;
      if(l<r && l==m){
  m = r;
      } else if(l<r && r==m) {
  m = l;
      }
   }
   return m;
}

void quick(int *array, int b_pos, int e_pos){
   if(e_pos-b_pos>1){
      int q = split(array,b_pos,e_pos);
      quick(array,b_pos,q);
      quick(array,q+1,e_pos);
   }
}

 

Revise this Paste

Your Name: Code Language: