-
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
-
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
-
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]
-
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 .