Please help, sorting related

This is a discussion on Please help, sorting related within the C++ Programming forums, part of the General Programming Boards category; Hi all, I've been working on a program that is supposed to sort and array in descending order using the ...

  1. #1
    Unregistered
    Guest

    Please help, sorting related

    Hi all,
    I've been working on a program that is supposed to sort and array in descending order using the bubble sort mechanism, but I just can't seem to get it. I've looked up some sites on the internet , and I searched this board but I still didn't get to anything. Below is the code I have reached to:

    ----------------------------------------------------------------------------
    #include <iostream.h>
    #include <fstream.h>
    int main ()
    {
    ifstream inFile ("insort.txt" , ios::in);

    int i, vector[20], max, dim,temp,k,mpos;

    inFile>>dim;

    for (i=0 ; i<dim ; i++)
    inFile>>vector[i];


    for (i=0 ; i<=dim-1 ; i++)
    for (k=i; k<=dim ; k++){
    if (vector[k]>max){
    max=vector[k];
    mpos=k;
    }

    temp=vector[mpos];
    vector[mpos]=vector[i];
    vector[i]=temp;
    }

    for (i=0 ; i<dim ; i++)
    cout<<vector[i]<<endl;



    return 0;

    }

    ------------------------------------------------------------------

    I can't figure out what's wrong with it.

    Help is most appreciated.
    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    30

    Try something like this

    This is a bubblesort that I wrote for one of my projects.
    it is written for Dev C++ so will probably need modifying.
    You will also need to modify the method of inputting your data into your arrays.

    Hope this helps...

    #include <stdio.h>
    #include <iostream.h> // include any header files you might need
    #include <fstream.h>
    #include <stdlib.h>

    int main(int argc, char *argv[]) // 'int main()' will do for most compilers.
    {

    bool swapped=false; // flag.
    int i, temp;
    int v[5] = {0,1,6,4,3}; // numbers to be sorted
    do
    {
    swapped = false;// reset swapped flag.
    for(i=0;i<4;i++) // only do to size of array - 2
    {
    if(v[i]>v[i+1]) // change this to '<' for descending sort
    {
    temp=v[i];
    v[i]=v[i+1]; // Swap them!
    v[i+1]=temp;
    swapped = true; // set swapped flag
    }
    }
    }while(swapped); // do...while loop exits when no more swaps have been made, ie the array is sorted.

    for(i=0;i<5;i++)
    {
    cout<<v[i]<<" "; // print it out to verify that it works.
    }
    system("pause"); // only required for Dev-C++

    return 0;
    }
    Homer

    D'OH!

    mmmmmmmm... iterations

  3. #3
    Unregistered
    Guest

    Smile Thank you !!!!!!!!

    Thank you very much Homer, you made my day!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM

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