help with sorting program

This is a discussion on help with sorting program within the C++ Programming forums, part of the General Programming Boards category; i need to write a program that will generate 50000 random number from 1-10000 and then sort them using three ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    help with sorting program

    i need to write a program that will generate 50000 random number from 1-10000 and then sort them using three different functions. Bubble Sort, Insertion Sort, and Selective Sort

    here is the code i have right now. i pretty much have the foundation layed out .. i think at least. i just need to tweak it to get it to work properly

    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];
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    How does it not work? Or are you just looking for improvement suggestions for code that works?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    if (a[j]>a[j+1]);
    Take a closer look of what this statement do... or does not do...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    well the code doens't work, and i figured that all of my funcitons weren't fully correctly written. also i don't know how to implement them into my program to have them run

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21