LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2004, 03:38 AM   #1
salahuddin_66
Member
 
Registered: Jan 2004
Location: Dhaka, Bangladesh
Distribution: Debian, Gentoo
Posts: 283

Rep: Reputation: 30
Cool buble sort


hello

could some one describe me how it works
i could not understand clearly

---------------------------------------
/* now short them using a buble sort */

for (a=1; a<count; ++a)
for (b=count-1; b>=a; --b){

/* compair adjacent elements */
if (item[b-1] > item[b]){

/* excage elements */
t= item[b-1];
item[b-1] = item[b];
item[b]=t;

---------------------------------------


Code:
 
#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
int item[100];
int a, b, t;
int count;

/*read in numbers*/

printf("how many numbers?");
scanf("%d", &count);

for(a=0; a<count; a++) scanf("%d", &item[a]);


/* now short them using a buble sort */

for (a=1; a<count; ++a)
   for (b=count-1; b>=a; --b){
   
   /* compair adjacent elements */
    if (item[b-1] > item[b]){
  
  /* excage elements */
   t= item[b-1];
   item[b-1] = item[b];
   item[b]=t;
   
   }
  
   }
   
   /* desplay stored list */   
   for (t=0; t<count; t++) printf("%d\t", item[t]);
     

return 0;

}
 
Old 08-30-2004, 04:28 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,

this is not a recommended method to sort elements. I would say that it can be used until 20/30 elements without headaches. It is the most primitive N^2 method. I suggest a look into Numerical Recipes book. Try to find Heapsearch or Quicksort for this kind of task.
What this code makes is perform a check per element couple and make the exchange if they are not sorted. one by one. most of the time this is not necessary. The easiest way to understand the code is a piece of paper and a pen. Write down some 5 numbers and you get how it works.

Regards

slackie1000

Last edited by slackie1000; 08-30-2004 at 04:30 AM.
 
Old 08-30-2004, 04:36 AM   #3
usercsr
Member
 
Registered: Sep 2003
Location: Little Rock, Arkansas
Distribution: Slackware-Current
Posts: 129

Rep: Reputation: 15
This might help in understanding:

Sorting applet
 
Old 08-30-2004, 04:38 AM   #4
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
Quote:
This might help in understanding:

Sorting applet
great link...

interesting..

regards

slackie1000
 
Old 08-30-2004, 11:12 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
bubble sort is a good algorithm if you're operating on data that's nearly sorted at the start. (Because it can abort when it makes a complete pass without swapping anything.) It's also in-place and the code is small, so it could be a good choice for sorting (small) sets in hardware.

Just thought I'd muddy the waters a bit in regard to the claim that it's a poor algorithm In general, I admit that it isn't that great.
 
Old 08-31-2004, 04:02 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
How bubble sort works!!!

Lets suppose you have and arry of 6 elements to sort.

3 6 2 9 1 5

and you are using bubble sort, it will be sorted as,

STEP 1:
3 6 2 9 1 5 if( item[b-1] > item[b] ) { \\swap }
So in this case the value of b is 5 so we can say if( item[4] > item[5] ) { \\swap }
Now item[4] == 1 and item[5] == 5 and the condition is false so no action will be taken and the array will remain same as it is. and the variable b is decremented to 4.

STEP 2:
3 6 2 9 1 5 if( item[b-1] > item[b] ) { \\swap }
So in this case the value of b is 5 so we can say if( item[3] > item[4] ) { \\swap }
Now item[3] == 9 and item[4] == 1 and the condition is true so action will be taken and the values are swaped and array becomes.
3 6 2 1 9 5
and the variable b is decremented to 3.

STEP 3:
3 6 2 1 9 5 if( item[b-1] > item[b] ) { \\swap }
So in this case the value of b is 5 so we can say if( item[2] > item[3] ) { \\swap }
Now item[2] == 2 and item[3] == 1 and the condition is true so action will be taken and the values are swaped and array becomes.
3 6 1 2 9 5
and the variable b is decremented to 2.

STEP 4:
3 6 1 2 9 5 if( item[b-1] > item[b] ) { \\swap }
So in this case the value of b is 5 so we can say if( item[1] > item[2] ) { \\swap }
Now item[1] == 6 and item[2] == 1 and the condition is true so action will be taken and the values are swaped and array becomes.
3 1 6 2 9 5
and the variable b is decremented to 1.

STEP 5:
3 1 6 2 9 5 if( item[b-1] > item[b] ) { \\swap }
So in this case the value of b is 5 so we can say if( item[0] > item[1] ) { \\swap }
Now item[0] == 3 and item[1] == 1 and the condition is true so action will be taken and the values are swaped and array becomes.
1 3 6 2 9 5

As the variable b ==1 and so the loop will end because the condition was b<=a and a==1. But we have a nested loop so again all these steps will be repeated but the loop will end when b==2 because now a is incremented from 1 to 2 by the outter loop.
You can observe that the after each itteration of outter loop (after all STEPS), Atleaset one element is sorted to its required place. As the element 1 was on 5th place and after these steps it comes to 1st place where it was supposed to be, and the next group of steps will bring element 2 to 2nd place and so on. So it requires maximum of N group of steps to sort the array as each group sorts atleast one element. And that thing is menaged by the outer loop.

I hope you have no problem in exchange function (swap).

Wish you all the best.
 
Old 08-31-2004, 04:44 AM   #7
salahuddin_66
Member
 
Registered: Jan 2004
Location: Dhaka, Bangladesh
Distribution: Debian, Gentoo
Posts: 283

Original Poster
Rep: Reputation: 30
thanx
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Sort() bendeco13 Programming 2 11-02-2005 11:26 PM
how to sort the output of ls bahadur Programming 18 03-28-2005 07:08 PM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 12:10 PM
frozen-buble: no hash reference rosschilen Linux - Software 3 09-05-2004 09:27 AM
sort pantera Programming 5 05-26-2004 07:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration