ALGORITHM:
STEP 1: Start the program.
STEP 2: Assign the pointer array *a[100] as global, Read the value of n.
STEP 3: Set a for loop to read the elements of array.
for(i = 0; i < n; i++)
STEP 4: Call the function sort(0, n - 1).
STEP 5: Print the sorted array a.
STEP 6: Stop the program
FUNCTION SORT (int first, int last)
STEP 1: Declare the local variable.
STEP 2: Check if first is less than last.
first < last
STEP 3: If so then assign the following
pivot = a[first]
i = first
j = last
STEP 4: Assign a while loop till the condition.
I < j
STEP 5: Assign a while loop to increment i till
a[i] < pivot and i < last
STEP 6: Assign a while loop to decrement j till
a[j] > pivot and j > first
STEP 7: Check whether i is than j if so then swap the values of a[i] and a[j]
temp = a[i]
a[j] = a[j]
a[j] = temp
STEP 8: Then swap the values of a[j] and a[first].
temp = a[j]
a[j] = a[first]
a[first] =temp
STEP 9: Call another functions sort(first, j - 1) and sort(j + 1, last)
SAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
int *a[50], n, i;
void sort(int, int);
void main()
{
clrscr();
printf(" Enter the no of elements: ");
scanf(" %d ", &n);
printf(" Enter the array elements one by one: ");
for(i = 0; i < n; i++)
scanf(" %d ", &a[i]);
printf(" \n UNSORTED ARRAY ELEMENTS ");
for(i = 0; i < n; i++)
printf(" \t %d ", a[i]);
sort(0, n - 1);
printf(" \n SORTED ARRAY ");
for(i = 0; i < n; i++)
printf(" %d ", a[i]);
getch();
}
void sort(int first, int last)
{
int *temp, *pivot, i, j;
if(first < last)
{
pivot = a[first];
i = first;
j = last;
while(i < j)
{
while((a[i] <= pivot) && (I < last))
i++;
while((a[j] >= pivot) && (j > first))
j--;
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[first];
a[first] = a[j];
a[j] = temp;
sort( first, j – 1 );
sort( j + 1, last );
}
}
OUTPUT:
Enter the number of elements: 6
Enter the array elements one by one
23
45
89
98
09
65
UNSORTED ARRAY ELEMENTS 23 45 89 98 09 65
SORTED ARRAY 09 23 45 65 89 98
ALSO READ:
-- Program To Understand Working Of Address Concept.
-- Program On Use Of Array & Pointer Concept Together.
-- Program On Pointers & 2 - Dimensional Array.
-- Program On Strings Into Array Of Pointers.
-- Program To Accept & Add Ten Numbers Using Pointers.
-- Program To Implement Minimum Spanning Tree Using Functions, Pointers & Structures.
-- Program To Implement 8 Queens Problem Using Arrays, Pointers & Functions.
-- Program To Implement Quick Sort Using Pointers, Functions & Arrays.
-- Program With Algorithm To Implement Bubble Sort Using Pointers.
-- Program To Implement Recursive Algorithm Using Pointers.
-- Program On Strings Into Array Of Pointers.
-- Program On Pointers & 2 - Dimensional Array.
-- Program On Use Of Array & Pointer Concept Together.
-- Program On Arithmetic Calculations Using Pointers.
-- Program To Add Two Numbers Using Pointers.
-- Program To Accept & Add Ten Numbers Using Pointers.
... Back To C Program Index.
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com