ALGORITHM:
STEP 1: Start the program.
STEP 2: 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 sel (a, 0, n – 1 )
STEP 5: Print the sorted array a.
STEP 6: Stop the program.
FUNCTION SEL (int *x, int start, int stop )
STEP 1: Declare the local variable.
STEP 2: Assign begin = start and small = begin and check if
start < stop.
STEP 3: If so then set a for loop
for(i = begin + 1 ; i <= stop; i++ )
STEP 4: Check the condition
x[i] < x[small]
STEP 6: If so then assign the value of i to small
small = i
STEP 7: Then swap the values of x[begin] and x[small] using temp
temp = x[begin]
x[begin] = x[small]
x[small] = temp
STEP 8: Call another function sel ( x, start + 1, stop )
SAMPLE PROGRAM:
#include<stdio.h>
#include<conio.h>
void sel(int *[],int,int);
int main()
{
int a[100], i, n;
clrscr();
printf(" \n Enter the number of elements: " );
scanf(" %d ", &n);
printf(" \n Enter the array elements one by one \n ");
for(i = 0; i < n; i++)
scanf(" %d ", &a[i]);
printf("\nUNSORTED ARRAY ELEMENTS");
for(i = 0; i < n; i++)
printf(" \t %d ", a[i]);
sel( a, 0, n – 1 );
printf(" SORTED ARRAY: \n ");
for(i = 0; i < n; i++)
printf(" \t %d ", a[i]);
getch();
return(0);
}
void sel(int *x[], int start, int stop)
{
int begin = start;
int small = begin;
int temp, i;
if(start < stop)
{
for(i = begin + 1; i <= stop; i++)
{
if( x[i] < x[small] )
small = i;
}
temp = x[begin];
x[begin] = x[small];
x[small] = temp;
sel(x, start + 1, stop);
}
}
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