hi! i'm having a hard time in understanding the "bubble sort", can anybody please help me in creating a programming that allows the user to input 10 integers and gives them an option whether it will display from "hi to low" or "low to hi". thanks in advance.

Recommended Answers

All 4 Replies

Try to program, we'll help if you get stuck.

//Assumptions :
//1)Data to be sorted is stored in an array
//2)Data to be sorted is of integer type
//3)Sorting is done in Ascending order.
Simple Logic :

int array[10]; //array of size 10 
int store;
for(int i = 0; i < array.length(); i++)
{
    for(j = i+1; j < array.length(); j++)
    {
         if (array[i] > array[j])
         {
             tmp = array[i];
             array[i] = array[j];  //swapping
             array[j] = tmp;
         }  
    }
}

//after this loop the array will have it's data sorted in ascending order.
As for options you can always use a while loop to make a 'Menu driven' program :)
Hope this helps :)

br
KKR

commented: Wow, another giveaway program! Ain't we just a wonderful code shop here! -3

here is the algo......

procedure bubbleSort( A : list of sortable items )
do
swapped = false
for each i in 1 to length(A) - 1 inclusive do:
if A[i-1] > A then
swap( A[i-1], A )
swapped = true
end if
end for
while swapped
end procedure

thanks a lot, just what i needed, a guide to write my program. this will help a loooot. :D

will get back you for my code. do you mind double checking it later for any error?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.