Bubble Sort coderinme

Bubble Sort coderinme

Bubble Sort coderinme
Starting from the beginning of the list, bubble sort compares every adjacent pair, swap their position if they are not in the right order. After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared. Let’s program for it in C

#include<stdio.h>
int b_sort(int x[], int size)
{
int pass, comp, temp;
for(pass=1; pass<=size-1; pass++)
for(comp=1; comp<=size-pass; comp++)
if(x[comp-1]>x[comp])
{
temp=x[comp-1];
x[comp-1]=x[comp];
x[comp]=temp;
}
return;
}
int main()
{
int i, size,k,j,num,tmp, int list[10];
printf("\nEnter size of list which is to be sorted: ");
scanf("%d",&size);
printf ("Enter % d elements of the list: \n",size);
tmp = size;
for(i = 0; i <= size - 1; )
{
k = 0;
scanf("%d",&num);
for( j = 0 ; j <= i ; j++)
{
if(num == list[j])
{
k++;
}
}
if(k == 0)
{
list[i] = num;
i++;
}
else
size--;
}
b_sort(list,size);
printf("\nThe sorted list is:\n");
printf("*******************************\n");
for(i=0; i<=size-1; i++)
{
printf("%d\n", list[i]);
}
printf("\nNUMBER OF DUPLICATES %d.\n",tmp-size);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Bubble Sort coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.