Is this homework? Have you characterized the complexity of the routine you have?
–
Michael BurrAug 13 '12 at 17:35

Doing it on my own. That's why I have not asked for the code, only algo names as it is similar to homework question.m:)
–
Prakhar Mohan SrivastavaAug 13 '12 at 17:39

@MichaelBurr just wondering if what way would the answers matter?
–
YuckAug 13 '12 at 17:46

@Артём Царионов: I think that people often approach answering homework-based questions in a different manner. For example, to avoid simply 'giving the answer' or to orient an answer more at fundamentals. As far as characterizing the complexity of the current routine - how can you know if you've improved things until you understand the current situation (at least somewhat)?
–
Michael BurrAug 13 '12 at 18:39

@MichaelBurr great point, i never understood why people care whether it's homework or not
–
YuckAug 13 '12 at 19:31

@PrakharMohan idk if it's enforced. From the link: Complexity Approximately N*logN comparisons on average (where N is last-first). In the worst case, up to N^2, depending on specific sorting algorithm used by library implementation. From "depending" I take it that the standard doesn't enforce a particular algorithm, but one that respects the complexity requirements.
–
Luchian GrigoreAug 13 '12 at 17:49

Your algorithm is selection sort, an O(n^2) algorithm: if the input size grows linearly in n, then the running time is proportional to a quadratic function of n. The minumum time complexity for comparison based sorts on arbitrary input (i.e. without prior knowledge about the input) is O(n log n). The STL function std::sort provides this guarantee.

From this code, it is also obivous how selection sort works: repeatedly find the minimum element in the remaining part of the array, and swap it into place. It should be equivalent to your own code, but I hope you agree it is much easier to understand (once you get to know the STL, that is).

Hi there! I don't wish to offend you with this comment, but: 1) you're not helping him by providing a full solution if this is for educational purposes. 2) the code is not valid, as C++ doesn't support variable-length arrays.
–
Luchian GrigoreAug 13 '12 at 17:36

You are using Selection sort to sort your array. The running time of this algorithm id O(n^2). You can use Merge sort or Heap Sort to sort which has running time of O(nlog(n)).
You can also use Intro Sort which uses a very neat trick to push QuickSort's worst case down to O(n log n) while maintaining the other good performance characteristics

In practice, quicksort is the most efficient.
–
Luchian GrigoreAug 13 '12 at 17:39

@LuchianGrigore: Quicksort has terrible worst-case for cases that are actually all too common. It's better to use a combination usually, like introsort.
–
Benjamin LindleyAug 13 '12 at 17:42

1

Actually, quicksort is no longer considered the average-case sorting king, though it was when I was in college. Now, both Python and Java have switched to timsort, which is a very nicely modified mergesort so that the merge steps are done in place.
–
user1277476Aug 13 '12 at 21:02