2

If I want to sort the second dimension's 0 element like that:

short arr[5];

arr[0][0] = 122;
arr[0][1] = 33;
arr[0][2] = 45;

arr[1][0] = 33;
arr[1][1] = 12;
arr[1][2] = 42;
.
.
.

It will sort arr[i][0], but arr[i][1] and arr[i][2] will come with arr[i][0] to a new element.

4
  • Use a C++ vector<> rather than a C-style array - then you can just apply the sort method. If you must use C-style arrays for some reason then use qsort() from cstdlib to sort. Commented Jul 23, 2010 at 21:29
  • 2
    @Paul: sort is a generic non-member function algorithm. It can be used with an array just as easily as it can be used with a vector. Commented Jul 23, 2010 at 21:31
  • 6
    You declared a one-dimensional array and are accessing it as a two-dimensional array? What are you trying to do here? Commented Jul 23, 2010 at 22:02
  • can you list (add to the question) what the output was for the 2x3 example and what the desired output is? Commented Aug 12, 2010 at 18:47

5 Answers 5

6

To sort a standard C array using the std::sort algorithm:

#include <algorithm>
    ...
    sort(&arr[0], &arr[0] + 5)

Now if you have a two dimensional array, you can use the same idea to sort the second dimension of each item in the first dimension:

short arr[5][5];
...
for(int i = 0; i < 5; ++i) {
   sort(&arr[i][0], &arr[i][0] + 5);
}
Sign up to request clarification or add additional context in comments.

Comments

4

This should help you : http://www.cplusplus.com/reference/algorithm/sort/

Comments

3

std::sort will, by default, sort objects in ascending order. To sort in descending order, you can use the std::greater function object:

std::sort(arr, arr + 5, std::greater<short>());

Comments

1

There are too many sorting algorithms, like quicksort and bubble sort, with different complexity. And this complexity varies upon size of data set, default order of data within data set, etc. You have to study your data set and pick one algorithm that will meet your requirement faster. You can find sorting algorithms in http://en.wikipedia.org/wiki/Sorting_algorithm.

1 Comment

Given that the standard library has sort (average case N log N) and stable_sort (worst case N (log N)^2), there is very rarely a good reason to implement your own sorting algorithm.
1

If this is a homework assignment, you probably don't want to use std::sort. Your teacher might consider you cheeky :P

I'd go with what Muhit said and try learning from Wikipedia's article on sorting algorithms, http://en.wikipedia.org/wiki/Sorting_algorithm. Most of the individual articles have the algorithms implemented in pseudocode, so you can just pick one to code up.

But if this is for a different project, yeah, definitely go with STL, there shouldn't really be any reason to code up a sorting algorithm by hand right now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.