Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,562 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,887 people online right now. Registration is fast and FREE... Join Now!




Program that sorts random numbers...

 
Reply to this topicStart new topic

Program that sorts random numbers...

wco5002
10 Apr, 2008 - 02:28 PM
Post #1

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 16

hey i have to work on a lab in c++ .. it deals with sorting, i need to generate 50,000 numbers from 1-100,000 and i then have to sort them using bubble, selection, and insertion sort .. here is what i have right now .. heres what i have so far

CODE

#include <iostream>
#include <ctime>
using namespace std;

void bubblesort (int temp, int size);

int main (){

const int size = 50000;
int temp[size];

for(int i =0; i<size; i++){
temp[i]=rand()%10000;
cout << temp[i] << endl;

bubblesort (temp, size);
cout << temp[i];

return 0;

}

}


void bubblesort (int a[], int n) {
   for (int i=1; i<n; i++)
      for (int j=0; j<n-1; j++)
         if (a[j]>a[j+1]);
        
}

User is offlineProfile CardPM
+Quote Post

pertheusual
RE: Program That Sorts Random Numbers...
10 Apr, 2008 - 03:51 PM
Post #2

D.I.C Head
**

Joined: 26 Jan, 2008
Posts: 231



Thanked: 3 times
My Contributions
Okay, well, first quick thing, you are generating random from 0-9999, not 1-100000. So, you forgot a 0 on the end and need to do add one to shift it.

Your brackets for your for loop also shouldn't go aroun everything, just rand and cout. The bubblesort call shouldn't be inside. Then you'll need a second for loop after the bubble sort to display the new sorted array.

As for Bubblesort, I'd say start by reading the pseudocode on wikipedia for the various algorithms. http://en.wikipedia.org/wiki/Bubble_sort#P..._implementation

Once you have a bit more of the algorithm down, we can help.

Per
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:43PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month