DS Bubble and Quick Sort
DS Bubble and Quick Sort
Algorithm
Step1: Get array size and array from the user.
Step2: In the array, in each iteration, adjacent elements
will be compared.
Step3: If the preceding element is larger than the
succeeding element, the two elements will be swapped. If
not, the next pair of elements will be compared.
Step4: The arr of iterations of the outer loop will be
one less than the size of the of the array.
Flowchart
Program
#include<stdio.h>
#include<conio.h>
Conclusion
Quick Sort
Algorithm
Step1: Get array length and array from the user.
Step2: Let the element at index posn 0 be the pivot
element and i, and the last element be j.
Step3: Increment i posn till j>pivot.
Step4: Decrement j posn till it finds an element smaller
than the pivot. Now swap i and j.
Step5: Continue till i and j cross each other.
Step6: Now all elements with value less than pivot come
before it and all elements with value greater than it
come after it. The pivot is now at its final posn.
Step7: Apply steps 2 to 6 to the two subarrays (less than
and greater than pivot) till you get a sorted array.
Flowchart
Program
#include<stdio.h>
int main()
{
int i, arrlen;
i=0;
printf("Enter number of elements: ");
scanf("%d",&arrlen);
int arr[arrlen];
while (i<arrlen)
{
printf("Enter %dth element of array: ",i);
scanf("%d",&arr[i]);
++i;
}
Quick_Sort(arr,0,arrlen-1);
printf("Your Quick Sorted array is: ");
for(i=0;i<arrlen;i++)
printf(" %d",arr[i]);
return 0;
}
Conclusion