Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Merge Sort
//clang 3.8.0 #include <iostream> void merge(int *A,int f,int m,int l){ int n1,n2,i,j,k; n1=m-f+1; n2=l-m; i=0; j=0; k=f; int L[n1],M[n2]; for(int i=0;i<n1;++i ) L[i]=A[f+i]; for(int j=0;j<n2;++j) M[j]=A[m+1+j]; while(i<n1&&j<n2){ if(L[i]>=M[j]){ A[k]=L[i++]; }else{ A[k]=M[j++]; } ++k; } while(i<n1){ A[k++]=L[i++]; } while(j<n2){ A[k++]=M[j++]; } } void mergeSort(int *A,int f,int l){ if(f<l){ int m=(f+l)/2; mergeSort(A,f,m); mergeSort(A,m+1,l); merge(A,f,m,l); } } void printArray(int *A,int len){ for(int i=0;i<len;++i ) std::cout<<A[i]<<' '; } int main() { int arr[]={1,3,4,2,7,9,5,6,0,8}; int len=sizeof(arr)/sizeof(arr[0]); mergeSort(arr,0,len-1); printArray(arr,len); }
run
|
edit
|
history
|
help
0
std::function copies
move_string
Division by zero exception example
vector flavors....
ternary test
SimpleList Example
Vector of pointer of P...
ArrayList Example Starter Code 2
general
Integer conversions