Thread: sorting arrays

  1. #1
    Unregistered
    Guest

    Unhappy sorting arrays

    I am currently taking a C level 2 course. The assignment is to create 4 arrays from information in a text file and sort through the arrays and print to a text file this information in descending order. This is the first time I have experienced arrays and I am having trouble writing code for this.

    I understand how to declare the arrays and I know I need to use a for statement to sort but I am having trouble with the idea of sorting them.

    The arrays are: employee ID, wage, hours, weekly pay. How do I sort the employee ID's into descending order and have the corresponding wage, hours, and weekly pay follow the ID number.

    Any information on this would be helpful to me as I seem to be struggling with this.

    Thanks

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    there are many swapping sequences [comparisons] you can use to sort the elements of your array... what you could do is look up things like the bubble sort or shell sort and implement these swapping sequences [comparisons] to your data arrays in order to sort them...

    hth
    hasafraggin shizigishin oppashigger...

  3. #3
    Unregistered
    Guest
    I recommend you to take a look at that:
    http://www.cpp-home.com/tutorial.php?113_1

    This is a tutorial about the selection sort... also, a code example of the selection sort, can be found here:
    http://www.cpp-home.com/code.php?82_1

    If you need C++ resources /tutorials, code, utilities and more.../, feel free to visit www.cpp-home.com

    Ilia Yordanov,
    www.cpp-home.com
    [email protected]

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    If you have a "stable" sorting algorithm, then, to use an example, if you sort wage, and then hours, and then dept. number, you'll get output something like this:

    Code:
    DEPT  #                         HOURS               WAGE
    9999999                        82                      23
    9999999                        82                      19
    9999999                        70                      90
    9999999                        70                      19
    9999999                        70                      12
    8273921                        100                    9
    8273921                        100                    8
    8273921                         80                     20
    7000000                         129                   12
    Otherwise, you'll have to sort the dept #s, then take all equal dept #s and sort them by their hours, and then take all equal ours and sort them by their wages. This takes more effort than just using a stable algorithm.



    Selection sort is the most common-sense sorting algorithm. Without having read anything about sorting, I decided I'd code a sorting algorithm, and that's what I came up with. Conversely, although bubble sort is very simple to code, it's not the most intuitive algorithm. Also, unless the data is close to being sorted already, bubble sort will be slooooooooow. The second kind of sorting algorithm I came up on my own I later found was like a bucket sort, except my system had no recursion, and each possible number had its own bucket! So it was very fast [O(n)], but took a lot of memory. This is far from being a general purpose sort, though. The best general-purpose sort is quicksort.

    My favorite site for information about common algorithms and data structures including those used for sorting and searching is http://ciips.ee.uwa.edu.au/~morris/Y...10/ds_ToC.html .

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, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. Sorting 2.. 1-D arrays of different types
    By GaGi in forum C Programming
    Replies: 3
    Last Post: 04-04-2002, 05:29 PM