Created
October 14, 2014 08:39
-
-
Save JesseEisen/485d6cfb4178af464ea0 to your computer and use it in GitHub Desktop.
some sort function snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//easy way | |
void quicksort(int v[], int n) | |
{ | |
int i, last; | |
if(n <= 1) | |
return; | |
swap(v,0, rand()%n); | |
last = 0; | |
for(i =1; i< n; i++) | |
{ | |
if(v[i] < v[0]) | |
swap(v,++last,i); | |
} | |
swap(v,0,last); | |
quicksort(v,last); | |
quicksort(v+last+1,n-last-1); | |
} | |
//branch search | |
int brachsearch(const int arr[], int low, int high,int key) | |
{ | |
int mid = low + (high - low)/2; | |
if(low > high) | |
return -1; | |
else | |
{ | |
if(arr[mid] == key) | |
return mid; | |
else if (arr[mid] > key) | |
branchsearch(arr,low, mid -1; key); | |
else | |
branchsearch(arr,mid+1,high,key); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment