473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

Bubble Sort

MMcCarthy
14,534 Expert Mod 8TB
The Bubble Sort is a very slow algorithm but it is one of the simplest which is why it is often used to introduce students to the concept of sorting.

Imagine you are looking at numbers on a screen. There is a line of numbers but the screen is so small you can only see 2 numbers at any one time. Let the amount of numbers be n and if we take the first number as being position 0 then the last number is in position n-1.

Now to start with we compare the number in position 0 with the number in position 1. If the number in position is bigger than the number in position 2 then you swap the postion of the numbers, so that the number that was in position 1 is now in position 0 and the number that was in position 0 is now in position 1.

However, if the number in position 0 is not bigger or is equal to the number in position 1 then leave them as they are. Now look at the numbers in position 1 and position 2 and repeat the process. Move down the line of numbers until you compare the last two in positions n-2 and n-1. Once you have completed the process with these two numbers you will be able to conclude that the largest number is now in position n-1. The number in position n-1 is therefore sorted and will not have to be included in the next run through the line.

You now start again at position 0 and position 1 and compare them again. You will run through the line as before but this time you will stop when you compare the numbers in position n-3 and n-2 (as n-1 is in the correct position). At the end of this line check you will know that n-2 is now in it's sorted position as the second biggest number and will not have to be checked again.

Continue these line searches until you are only left with position 0 and position 1. If you don't know how many numbers are in the line then these positions can also be known as n-(n) and n-(n-1).

Step 1: Create an array of a number variable to hold the line of numbers.
Step 2: Create a FOR...LOOP starting at position n-1 and decreasing by 1 for each line search.
Step 3: Create a FOR...LOOP nested in the previous FOR...LOOP to run through the numbers in the array until you reach the position of the last unsorted number which will be controlled by the outer FOR...LOOP .
Step 4: Create an IF statement to compare the two positions in the array and swap them if required.
Feb 19 '07 #1
2 9772
jbiet
1
please also explain about heap sort merge sort and all types of sorts in data structures
Sep 2 '07 #2
Ganon11
3,652 Expert 2GB
There's a wonderful article in the Java section about a generic heap sort - I imagine it wouldn't be too difficult to translate this into C++.
Sep 3 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
9
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h>...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.