473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

What am I doing wrong with this sort?

I am trying to sort an array of numbers from greatest to least using a bubblesort thingy but it wont work for me heres the code I have...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. class sort
  4. {
  5. public:
  6.     void BubbleSort (int arr[], int length)
  7.     {
  8.         bool swap= true;
  9.         int temp;
  10.         for (int i = 0; swap; i++)
  11.         {
  12.             if(arr[i] > arr[i + 1])
  13.             {
  14.                 temp = arr[i];
  15.                 arr[i] = arr [i + 1];
  16.                 arr [i+1] = temp;
  17.                 swap = true;
  18.             }
  19.         }
  20.     }
  21. };
  22. int main()
  23. {
  24.     sort* mySort = new sort;
  25.     int a[] = {4, 14, 5, 0, 3, 23, 7, 16, 26, 34, 45, 54, 1, 9, 6, 15, 20, 8, 32, 86};
  26.     (*mySort).BubbleSort ((a),(sizeof(a)/4));
  27.     for (int i = 0; i < (sizeof (a) / 4); i++)
  28.     {
  29.         cout << a[i] << ", ";
  30.     }
  31.     cout << "\n";
  32.     return 0;
  33. }
  34.  
  35.  
I am probably making a stupid mistake so dont laugh :P
Jul 26 '08 #1
2 1058
For one thing, your swap variable is always true, it's initialized to true, and nothing in your code ever makes it false, so the way you use it as a loop control variable means that you're going to get an infinite loop.

Also, even though you pass the length of the array to the sort function you never actually use it. length should be the loop control variable, the swap variable isn't necessary.

Finally, a bubble sort requires a nested pair of loops, in other words a loop inside a loop. It should look something like this:

Expand|Select|Wrap|Line Numbers
  1. void BubbleSort(int arr[], int length)
  2. {
  3.     for(int i = 0; i < length; i++)
  4.     {
  5.         for(int j = i; j < length; j++)
  6.         {
  7.             if(arr[j] > arr[i])
  8.             {
  9.                 int temp = arr[i];
  10.                 arr[i] = arr[j];
  11.                 arr[j] = temp;
  12.             }
  13.         }
  14.     }
  15. }
  16.  
The outer loop keeps track of how much of the list is sorted (in other words i is the index of the first unsorted element). The inner loop passes over the unsorted part of the list looking for the largest element in the unsorted part of the list and swaps it to the front of the unsorted portion. Once the inner loop finishes, the first element of the unsorted portion is now sorted and the outer loop moves ahead to the next unsorted element and repeats the process.
Jul 26 '08 #2
Thank you very much...
Jul 27 '08 #3

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
28
by: john_sips_tea | last post by:
Just tried Ruby over the past two days. I won't bore you with the reasons I didn't like it, however one thing really struck me about it that I think we (the Python community) can learn from. ...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
9
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
13
by: Zhou Yan | last post by:
I am a novice, I want to know what is CPP, as showed in title. I heared about it but the book I read do not mentioned it. Maybe this book is too "introductory". Any one can give me a concept about...
0
by: Pat | last post by:
Hi Guys, Please help me. I am having a strange issue. Not sure if I am doing something wrong or this is a bug. This is a sample code. This contains 2 text boxes(txtName, txtPhone) and 2 buttons...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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 projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.