Turbo Sort(Easy problem)

I used bubble sort and insertion sort to solve this problem(using c++) but in both cases it showed “Time limit exceeded”. I also replaced cin, cout with printf, scanf to quicken up things but still the same result. What should I do?

Try problem with other sorting techniques like merge , quick or else go with internal sort method available in c++ (which is part of STL library) happy coding :slight_smile:

1 Like

For common algorithms use STL functions to solve the problems. By common algorithms, I mean sorting, searching, list, stack and queue and so on.

for your code, use sort(arr,arr+size) and then print the new array.

Happy coding.

You will also use sort algorithm. Which is define in algorithm header file or you will also use bits/stdc++ header file .

For example -

 http://ideone.com/oYlGTw 

Time complexity plays an essential role in the algorithmic world. If you don’t know the time complexity, I’d refer to this Wikipedia article

Anyways, Bubblesort, Insertion Sort and Selection sort all have O(n^2) as the worst time complexity. The average complexity won’t be useful in the Competitive Programming world because problem setters always (hopefully) make worst case test data.

Better use the internal sort algorithm of C++, which is guaranteed to be O(n log n).

#include <algorithm>
int A[1000];
int N;
int main(){
  A[N++] = 1;
  A[N++] = -5;
  A[N++] = 10;
  std::sort(A, A + N); // afterwards -5 1 10
}
2 Likes