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

    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;


    for (i=0 ; i<dim ; i++)

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


    for (i=0 ; i<dim ; i++)

    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

    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
    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
    v[i]=v[i+1]; // Swap them!
    swapped = true; // set swapped flag
    }while(swapped); // do...while loop exits when no more swaps have been made, ie the array is sorted.

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

    return 0;


    mmmmmmmm... iterations

  3. #3

    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, 12:27 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  3. Doxygen failing
    By Elysia in forum A Brief History of
    Replies: 19
    Last Post: 04-16-2008, 02:24 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM

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