Created
January 16, 2016 16:12
-
-
Save Iruyan-Zak/f44913f7536677f1e260 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define N 10000 | |
void swap(int *a,int *b) | |
{ | |
int temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
int search_big_num(int n, int a[], int pivot) | |
{ | |
printf("big.\n"); | |
for(int dbg=0; dbg<n; dbg++) | |
printf("%d ", a[dbg]); | |
putchar('\n'); | |
for(int i=0; i<n; ++i){ | |
if(a[i] > pivot) return i; | |
} | |
return -1; | |
} | |
int search_small_num(int n, int a[], int pivot) | |
{ | |
printf("small.\n"); | |
for(int dbg=0; dbg<n; dbg++) | |
printf("%d ", a[dbg]); | |
putchar('\n'); | |
for(int i=0; i<n; ++i){ | |
printf("%d, ", a[n-i-1]); | |
if(a[n-i-1] < pivot) return i; | |
} | |
return -1; | |
} | |
int _quicksort(int n, int a[]) | |
{ | |
int big, small; | |
for(int i=0, j=n-1, pivot=a[n/2]; ; ){ | |
if((big = search_big_num(j-i+1, a+i, pivot)) == -1) return j; | |
i += big; | |
if((small = search_small_num(j-i,a+i+1,pivot)) == -1) return i; | |
j -= small; | |
swap(&a[i],&a[j]); | |
} | |
} | |
void quicksort(int n, int a[]) | |
{ | |
if(n<=1) return; | |
for(int i=0; i<n; i++) | |
printf("%d ",a[i]); | |
putchar('\n'); | |
getchar(); | |
int i = _quicksort(n, a); | |
printf("i >> %d\n", i); | |
quicksort(i,a); | |
quicksort(n-i,a+i); | |
} | |
int main() | |
{ | |
int a[]={4,7,2,7,1,4,0,10,2,6,1}; | |
int n=11,i; | |
for(i=0; i<n; i++) | |
printf("%d ",a[i]); | |
putchar('\n'); | |
quicksort(n,a); | |
for(i=0; i<n; i++) | |
printf("%d ",a[i]); | |
putchar('\n'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これ同時に走査してかないとiとjとが交差した判定が出来ないから無理だよ…
僕の読み方が間違ってなければ…