?
Solved

bubble sort program sorting numbers in ascending & descending order

Posted on 2001-07-10
6
Medium Priority
?
1,389 Views
Last Modified: 2007-12-19
How to solve a problem where the program must arrange integers input by the user in ascending or descending order. The user is prompted to input an amount of integers to be sorted & then the actual numbers. This program is a bubble sort problem.
0
Comment
Question by:valtay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:exelrud
ID: 6270268
What exactly is your doubt ? How to implement bubble sort ?
Seems like homework and is unfair for us to solve it for you...
0
 

Expert Comment

by:Dak_Programmer
ID: 6271198
bubble sort is done by going through the a list of n elements, n-1 number of times...

comparing each element with the element directly after it in the list.... then you must swap the two elements using a swap routine
0
 

Expert Comment

by:spewbuddy
ID: 6271542
if you are interested in some example code,
send me a request e-mail
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
awjackin35 earned 200 total points
ID: 6271972
A bubble sort will keep passing through the file, exchanging adjacent
elements that are out of order, continuing until the file is sorted. Bubble
sort's prime virtue is that it is easy to implement, but whether it is
actually easier to implement than insertion or selection sort is arguable.

Sedgewick Alogorithms

For each i from r-1, the inner(j) loop puts the minimum element among
the elements in a[i], ....a[r] into a[i] by passing from right to left
through the elements, compare, compare-exchange successive elements. The
smallest one moves on all such comparisons, so it "bubbles" to the beginning.
As in selection sort, as the index i travels from left to right through
the file, the elements to its left are in their final position in the array.

template <class Item>
void bubble(Item a[], int 1, int r)
{ for(int i = 1; i < r; i++)
     for(int j = r; j > i; j--)
         compexch(a[j-1], a[j]);
}

Here is the header and implementation file I did for a programming
assignment for four different sorts. I could not find the client file
but these two files are correct.

//Header
//functions definitions in the class
class quick
{
public:
void Quicksort(  int vec[], int loBound, int hiBound );
void BubbleSort(  int vec[],int vSize );
void SelectionSort( int vec[],int vSize );
void Insertionsort(int vec[], int vSize);
private:
};

//Implementation file
#include"sortheader.h";

void quick::Quicksort(  int vec[], int loBound, int hiBound )
{    
      int pivot;    
      int loSwap;    
      int hiSwap;    
      int temp;
      //Swap function
      if (hiBound-loBound == 1)
      {             // Two items to sort        
            if (vec[loBound] > vec[hiBound])
            {            
                  temp = vec[loBound];            
                  vec[loBound] = vec[hiBound];            
                  vec[hiBound] = temp;        
            }        
            return;    
      }
      //Divides in half and swaps and sorts the values.
      pivot = vec[(loBound+hiBound)/2];          
      vec[(loBound+hiBound)/2] = vec[loBound];    
      vec[loBound] = pivot;    
      loSwap = loBound + 1;    
      hiSwap = hiBound;    
      do
      {            
            //Swap        
            while (loSwap <= hiSwap && vec[loSwap] <= pivot)            
                  loSwap++;        
            while (vec[hiSwap] > pivot)            
                  hiSwap--;        
            if (loSwap < hiSwap)
            {            
                  temp = vec[loSwap];            
                  vec[loSwap] = vec[hiSwap];            
                  vec[hiSwap] = temp;        
            }    
      } while (loSwap < hiSwap);    
      vec[loBound] = vec[hiSwap];    
      vec[hiSwap] = pivot;    
      if (loBound < hiSwap-1)        
            Quicksort(vec, loBound, hiSwap-1);    
      if (hiSwap+1 < hiBound)        
            Quicksort(vec, hiSwap+1, hiBound);
}

//Bubble sort
//////////////////////////////////////////////////////////////////////
void quick::BubbleSort(int vec[],int vSize )
{    
      //Definitions of the indexes and parameters
      int bottom;    
      int lastSwapIndx;    
      int i;    
      int temp;    
      bottom = vSize - 1;
      
      //Bottom is one less then the vector size
      while (bottom > 0)
      {    
            //starts the loop to compare thee bottom value with the
            // one above it.
            lastSwapIndx = 0;        
            for (i = 0; i < bottom; i++)            
                  if (vec[i] > vec[i+1])
                  //Swap sort sot swap lower number with higher number
                  {                
                        temp = vec[i];
                        vec[i] = vec[i+1];                
                        vec[i+1] = temp;                
                        lastSwapIndx = i;            
                  }        
            bottom = lastSwapIndx;    
      }
}

//Selection Sort
/////////////////////////////////////////////////////////////////////
//  Algorithm: straight selection sort
//After the sort finds the value to be moved it moves it with a single
//swap
void quick::SelectionSort(int vec[],int vSize )
{    
      int maxIndx;    
      int bottom;    
      int i;    
      int temp;    
      for (bottom = vSize-1; bottom >= 1; bottom--)
      {        
            maxIndx = 0;        
            for (i = 1; i <= bottom; i++)            
                  if (vec[i] > vec[maxIndx])                
                        maxIndx = i;        
          //swap
            temp = vec[bottom];        
            vec[bottom] = vec[maxIndx];        
            vec[maxIndx] = temp;    
      }
}

//Insertion Sort
//////////////////////////////////////////////////////////////////////
//On each pass of the main loop it inserts the next element in its
//correct position. This sort shifts elements over to make room for
//each new value.
void quick::Insertionsort(int vec[], int vSize)
{
      int temp;
      for (int i=1; i < vSize; i++)  
      {
            temp = vec[i];    
            for (int j=i; j > 0 && vec[j-1] > temp; j--)      
                  vec[j] = vec[j-1];    
            vec[j] = temp;  
      }
}

0
 
LVL 11

Expert Comment

by:griessh
ID: 6825692
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "awjackin35" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6875105
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question