Help sorting arrays

This is a discussion on Help sorting arrays within the C++ Programming forums, part of the General Programming Boards category; I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort. Anyone know of some good ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Help sorting arrays

    I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
    Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
    Thanks!

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    11
    Here's bubble sort, but you're better off doing the insertion sort since it's more useful
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int a[] = {5,4,7,6,3,7,1};
    
        // Simple bubble sort
        for (int i = 0; i < 7; i++)
        {
            for (int j = i; j < 7; j++)
            {
                if (a[j] < a[i])
                {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    
        for (int k = 0; k < 7; k++)
            cout<< a[k] <<' ';
    
        cout<<endl;
    }
    However, since I won't give you the code for all of them, you can find info and implementations here.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    That really helps a lot, thank you both for your help, i'll try it out now!

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

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