Solved programming problem

How to Sort an Array Using Replacement Method in C.

Source code

#include <stdio.h>

int main()

{

    int array[100],i,j,n,max,swap;

    printf("Please Enter Array Size: ");

    scanf("%d",&n);

    printf("Please Enter %d Elements of The array: ",n);

    for(i=0; i<n; i++)

    {

        array[i]=rand()%100;/// randomly any value will be generated

    }

    printf("Before: ");

    for(i=0; i<n; i++)

    {

        printf("%d ",array[i]);

    }

    for(i=0; i<n; i++)   ///its better to use i<n-1;

    {

        for(j=i; j<n; j++)   ///replacement sorting /// its better to is use j=i-1;

        {

            if(array[i]>array[j])

            {

                swap = array[i];

                array[i] = array[j];

                array[j]= swap;

            }

        }

    }

    printf("After: ");

    for(i=0; i<n; i++)

    {

        printf("%d ",array[i]);

    }

    return 0;

}

Output

Please Enter Array Size: 5


Please Enter 5 Elements of The array:


Before:


41 67 34 0 69


After:


0 34 41 67 69

Join the discussion

Add your comment