#include <stdio.h>
#include <stdlib.h> //Call random functions
voidSwap (doublearr[], inta, intb) // a,b swap function
{
double temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
int Partition(double arr[], int left, int right)
{
float pivot = arr[left]; // position of pivot starts at leftmost
int low = left + 1;
int high = right;
// repeat (low <= high) // until crossing
{
while (pivot >= arr[low] && low <= right) // process of finding values greater than pivot
{
low++; // move low to the right
}
while (pivot <= arr[high] && high >= (left + 1)) // the process of finding a value less than the pivot
{
high--; // move high to the left
}
If (low <= high)// If it is not crossed, run the swap process
{
Swap (arr, low, high); //low and high
}
}
Swap(arr, left, high); // Swap pivot with target pointed by high
return high; // return the location information of the moved pivot
}
void QuickSort(double arr[], int left, int right)
{
if (left <= right)
{
int pivot = partition (arr, left, right); // divided into two
QuickSort (arr, left, pivot - 1); // align the left area.
QuickSort (arr, pivot + 1, right); // Align the right area.
}
}
int main()
{
double arr[100];
int n, i;
printf("Please enter the number of data: ");"
scanf("%d", &n);
printf("\n");
printf("\n");
printf("Please enter data.");
printf("\n");
for (i = 0; i < n; i++) {
printf ("%dth data:", i + 1);
scanf("%f", &arr[i]);
}
QuickSort(arr, 0, n - 1);
printf ("arrangement after alignment :");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
I made the code like this, but I don't know where the problem is... If you run it, it can't be sorted
c quick-sort
When receiving the input value with scanf()
,
If you want to put it in a double
type container, you must receive it as "%lf"
.
scanf("%lf", &arr[i]);
© 2024 OneMinuteCode. All rights reserved.