Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is sorting a string O(n log n)? [duplicate]

Tags:

Possible Duplicate:
Plain English explanation of Big O

In the answer to a programming puzzle it said sorting a string takes O(n log n) time. How is that derived?

Does anybody have a good reference link for Big O resources.

Thanks

like image 754
Percy Flarge Avatar asked Dec 13 '10 22:12

Percy Flarge


People also ask

Why is the Big O complexity of a merge sort is O n log n?

Mergesort is a divide and conquer algorithm and is O(log n) because the input is repeatedly halved.

How is sorting n logn?

It essentially follows two steps: Divide the unsorted list into sub-lists until there are N sub-lists with one element in each ( N is the number of elements in the unsorted list). Merge the sub-lists two at a time to produce a sorted sub-list; repeat this until all the elements are included in a single list.

What does sorting a string do?

Sort string in C++Organizing or arranging a group of characters in a definite order i.e, ascending or descending based on their ASCII values is known as sorting a string. The output of a sorting program produces a reordered input or its permutation.

Which sorting has log n complexity?

Practical general sorting algorithms are almost always based on an algorithm with average time complexity (and generally worst-case complexity) O(n log n), of which the most common are heapsort, merge sort, and quicksort.


1 Answers

Why is sorting a string O(n log n)?

Sorting the characters in a string is not necessarily O(n log n).

  • O(n log n) is the optimal value for a comparison sort. It is also the complexity of many languages' default sort implementation. However it's certainly possible to do worse than this. The complexity of sorting the characters in a string depends on the specific algorithm you choose to solve this task.
  • It's also possible to do better than O(n log n) in some cases by using a sorting algorithm which is not a comparison sort. For example, if you know that you have an ASCII string with a maximum of 127 distinct characters you could use a counting sort which is O(n). A counting sort would also be feasible for Unicode strings where all characters are in in the Basic Multilingual Plane.
like image 167
Mark Byers Avatar answered Sep 21 '22 03:09

Mark Byers