GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jan-2009, 17:26
chengs chengs is offline
New Member
 
Join Date: Nov 2008
Posts: 3
chengs is on a distinguished road
Question

Anyone know about bubble sorting?


The program works so far... but how do I add descending part in the program?
CPP / C++ / C Code:
#include <iostream>
using namespace std;
void input (int ulist[26], int& n);
void Bubblesort (int ulist[26], int slist[26], int n);
void print(int list[26], int n);
double average;int n,sum;
void main()
{
	int ulist[26], slist[26];
	input(ulist, n);
	cout << "Unsorted" ;
	print(ulist, n);
	cout << "Sorted";
	Bubblesort (ulist,slist,n);
	print(slist,n);
}
void input(int ulist[26], int& n)            
{
	int i(0),value;
	cout << "enter value : \n";
	cin >> value ;

	while (i < 26 && value != -999)
	{
		i++;
		ulist[i] = value;
		if (i<26)
		{
		cin >> value ;
		}
	}
	n=i;
}
void Bubblesort(int unlist[26], int sortlist[26], int n)
{
	int i,j,temp;
	for (i=1;i<=n;i++)
			sortlist[i] = unlist [i];
	for (j=1;j<=n-1;j++)
		for (i=1;i<=n-j;i++)
			if (sortlist[i] > sortlist[i+1])
			{
				temp = sortlist[i];
				sortlist[i] = sortlist[i+1];
				sortlist[i+1] = temp;
			}
}
void print(int list[26], int n)
{
	int i;
	sum = 0;
	cout << " list of numbers are : \n";
	for (i=1; i<=n; ++i)
	{
		cout << list[i] << '\n';
		sum = sum + list[i];
	}
		average = sum/n;
		cout << "\n\n";
	    cout << "Average is "<< average << "\n";
}
Last edited by LuciWiz : 26-Jan-2009 at 17:44. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 26-Jan-2009, 19:54
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Anyone know about bubble sorting?


Quote:
Originally Posted by chengs
...how do I add descending part in the program?

Let me answer your question with another question. Given the following line quoted from your code:
CPP / C++ / C Code:
	if (sortlist[i] > sortlist[i+1])
What would happen if you reverse the inequality?

For bonus points, creating two functions which perform the opposing swap (one for an ascending sort, & the other for a descending sort...) can be passed to a generalized version of your sorting routine meaning you only have to write Bubblesort once.
  #3  
Old 31-Jan-2009, 00:22
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: Anyone know about bubble sorting?


try this

CPP / C++ / C Code:
void sort(int g[], int s[], int b[], int index[], int n){
	int i, temp, pass, flip = 1;

	for (pass = 0; (pass < n - 1) && (flip == 1); pass++){
	    flip = 0;
	    for (i = 0; i < n-1; i++){
		if (g[index[i]] + s[index[i]] + b[index[i]] < g[index[i + 1]] + s[index[i + 1]] + b[index[i + 1]]){
		   flip = 1;
		   temp = index[i];
		   index[i] = index[i + 1];
		   index[i + 1] = temp;
		}
	    }
	}
   }

well modify the variables.. haha
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a vector acosgaya C++ Forum 2 10-Jan-2008 11:54
array bubble sorting killer9012 C++ Forum 35 18-Oct-2006 09:59
Integer Sorting using Pointers. nikhilthemacho C Programming Language 9 28-Jul-2006 13:19
Bubble Sorts with 2d Arrays y2l C Programming Language 5 14-Apr-2006 14:01
Problem with sorting algorithm MM4o C++ Forum 3 24-Feb-2006 10:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.