Program to Implement Quick Sort
/**
The quick sort is an in-place, divide-and-conquer, massively recursive sort. The efficiency of the algorithm is majorly impacted by which element is choosen as the pivot point.The worst-case efficiency of the quick sort, O(n2), occurs when the list is sorted and the left-most element is chosen.*/
Pros: Extremely fast.
Cons: Very complex algorithm, massively recursive.
#include
#include
void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);
int numbers[5];
int main()
{
int i;
printf("Enter the numbers to be sorted");
for (i = 0; i <5; i++)
scanf("%d",&numbers[i]);
//perform quick sort on array
quickSort(numbers, 5);
printf("List of Elements after Sorting.\n");
for (i = 0; i < 5; i++)
printf("%i\n", numbers[i]);
}
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
static int count=0;
count++;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}
The quick sort is an in-place, divide-and-conquer, massively recursive sort. The efficiency of the algorithm is majorly impacted by which element is choosen as the pivot point.The worst-case efficiency of the quick sort, O(n2), occurs when the list is sorted and the left-most element is chosen.*/
Pros: Extremely fast.
Cons: Very complex algorithm, massively recursive.
#include
#include
void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);
int numbers[5];
int main()
{
int i;
printf("Enter the numbers to be sorted");
for (i = 0; i <5; i++)
scanf("%d",&numbers[i]);
//perform quick sort on array
quickSort(numbers, 5);
printf("List of Elements after Sorting.\n");
for (i = 0; i < 5; i++)
printf("%i\n", numbers[i]);
}
void quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
void q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
static int count=0;
count++;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}
WILL U PLEASE CINVERT THIS PROGRAM INTO JAVA CODE BY NOT USING MORE THAN 2 CLASSES& IMPLEMENT IT BY RECURSION
Posted by B.RAJESWARA REDDY | 11:31 PM
PLEASE MIL YOUR PROGRAM TO b.rajesh462@gmail.com AS EARLY AS POSSIBLE. THANKING U,
YOURS FAITHFULLY,
B.Rajeswara Reddy.
Posted by B.RAJESWARA REDDY | 11:35 PM