fix: quick sort

This commit is contained in:
kemasama 2022-04-25 20:16:09 +09:00
parent b1e74292c4
commit f0f5ffaf5e
2 changed files with 51 additions and 1 deletions

BIN
quick_sort/q_sort Executable file

Binary file not shown.

View File

@ -60,6 +60,7 @@ int right
return j; return j;
} }
/*********************************** /***********************************
***********************************/ ***********************************/
@ -82,6 +83,55 @@ int right
return 0; 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); int length = sizeof(array) / sizeof(int);
printf("Length: %d\n", length); printf("Length: %d\n", length);
q_sort(array, 0, length); quick_sort(array, 0, length);
int i ; int i ;