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

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




need help writing a program using sorting functions

 
Reply to this topicStart new topic

need help writing a program using sorting functions

wco5002
13 Apr, 2008 - 06:58 PM
Post #1

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 16

here is what i need to do:

Generate 3 lists of 50000 random numbers between 0 and 10000.
Use the three sorting algorithms (Bubble, Selection, and Insertion) to sort the three lists. Code is in the lecture slides.
And print the median value of each algorithm.

Now I want you to use the time() class, to calculate how much time each of the three algorithms take.
You can do this by recording the time before and again after the sorting algorithm call. and then taking the difference between the two times.

Example:
time_t timeOne = time();
// call the sorting algorithm
time_t timeTwo = time();
// and calculate the difference, look up the function on the C++ reference page.

now answer these questions :
Which was the slowest sorting algorithm?
Why is it the slowest?


here is my code so far, where do i go from here:
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]);
        
}


void insertionsort (double list[], int arraysize){
    for (int i=1; i<array size; i++) {
        double currentelement = list[i];
        int k;
        for (k=i - 1; k>=0 && list[k]> currentelement; k--){
        }
        list [k + 1] = currentelement;
    }
}

void selectionsort(int list[], int arraysize){
    for(int i=0; i<arraysize; i++){
        int min=list[i], index=i;
        for(int j =i; j<arraysize; j++){
            if(list[j]>min){
                index=j;
                min=list[j];
            }
        }
        swap(&list[i], &list[index];
    }
}

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Need Help Writing A Program Using Sorting Functions
13 Apr, 2008 - 06:59 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,435



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Why do you have your return in your for loop?
User is online!Profile CardPM
+Quote Post

wco5002
RE: Need Help Writing A Program Using Sorting Functions
13 Apr, 2008 - 07:02 PM
Post #3

New D.I.C Head
*

Joined: 25 Mar, 2008
Posts: 16

that was a typo...any suggestions though?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:25PM

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