Sunday 10 January 2016

Bubble Sort - Ascending Order (Algorithm 4.4)


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
int main()
{
    int N;
    scanf("%d", &N);
    int DATA[N], K, i, PTR, TMP;
    for(i=0; i<N; i++) scanf("%d", &DATA[i]);
    for(K=1; K<N; K++)
    {
        for(PTR=0; PTR<N-K; PTR++)
        {
            if(DATA[PTR]>DATA[PTR+1])
            {
                TMP = DATA[PTR];
                DATA[PTR] = DATA[PTR+1];
                DATA[PTR+1] = TMP;
            }
        }
    }
    for(i=0; i<N; i++) printf("%d\t", DATA[i]);
    printf("\n");
    return 0;
}
First input N = array element number.  ex. 5
Second input LA[i] = input N element one by one. ex. 55 44 33 22 11.
Output ex. 11 22 33 44 55


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.