fix: quick sort
This commit is contained in:
parent
b1e74292c4
commit
f0f5ffaf5e
BIN
quick_sort/q_sort
Executable file
BIN
quick_sort/q_sort
Executable file
Binary file not shown.
@ -60,6 +60,7 @@ int right
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
/***********************************
|
||||
クイックソートを行う
|
||||
***********************************/
|
||||
@ -82,6 +83,55 @@ int right
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***********************************
|
||||
クイックソートを行う
|
||||
***********************************/
|
||||
int
|
||||
quick_sort(
|
||||
int *array,
|
||||
int left,
|
||||
int right
|
||||
)
|
||||
{
|
||||
int right_index, left_index, pivot;
|
||||
|
||||
if ( left >= right )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
left_index = left;
|
||||
right_index = right;
|
||||
pivot = left;
|
||||
|
||||
for ( ; left_index < right_index ; )
|
||||
{
|
||||
for ( ; array[left_index] < array[pivot] ; )
|
||||
{
|
||||
left_index++;
|
||||
}
|
||||
|
||||
right_index--;
|
||||
|
||||
for ( ; array[right_index] > array[pivot] ; )
|
||||
{
|
||||
right_index--;
|
||||
}
|
||||
|
||||
if ( left_index < right_index )
|
||||
{
|
||||
swap(&array[right_index], &array[left_index]);
|
||||
}
|
||||
}
|
||||
|
||||
swap(&array[pivot], &array[right_index]);
|
||||
|
||||
quick_sort(array, left, right_index - 1);
|
||||
quick_sort(array, right_index + 1, right);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***********************************
|
||||
メイン関数
|
||||
***********************************/
|
||||
@ -102,7 +152,7 @@ main()
|
||||
int length = sizeof(array) / sizeof(int);
|
||||
|
||||
printf("Length: %d\n", length);
|
||||
q_sort(array, 0, length);
|
||||
quick_sort(array, 0, length);
|
||||
|
||||
int i ;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user