How reason that an algorithm has n*logn time complexity?

Click For Summary
SUMMARY

The discussion centers on understanding the time complexity of the quicksort algorithm, specifically its O(n*logn) best-case scenario, which is attributed to its divide-and-conquer nature. However, it is emphasized that not all divide-and-conquer algorithms exhibit this time complexity. To accurately analyze the time complexity of quicksort, one can either implement counters to track operations during execution or derive a mathematical equation based on the algorithm's structure. Additionally, the conversation touches on merge sort as an alternative, noting its higher memory usage compared to quicksort.

PREREQUISITES
  • Understanding of quicksort algorithm and its implementation
  • Familiarity with time complexity analysis
  • Knowledge of divide-and-conquer algorithms
  • Basic concepts of recursion and nested loops
NEXT STEPS
  • Research the mathematical derivation of quicksort's time complexity
  • Learn about merge sort and its memory implications
  • Explore techniques for analyzing recursive algorithms
  • Study the impact of pivot selection on quicksort performance
USEFUL FOR

Students, software developers, and computer scientists interested in algorithm analysis, particularly those focusing on sorting algorithms and their complexities.

Pithikos
Messages
55
Reaction score
1
Some time ago I wrote an exam where I had to write a pseudocode to sort elemens in a list. I used a quicksort with the pivot in the middle and wrote that the algorithm has time complexity in best case O(n*logn) because it is a divide-and-conquer algorithm. That didn't satisfy my teacher and he noted to me that not all divide-and-conquer algorithms have n*logn.
So how would someone reason about this? (Taking quicksort as en example)
 
Technology news on Phys.org
You'd either have to add counters for the number of operations done in quicksort program and actually run it with some test sets of data, or generate a mathematical equation for the number of operations done by analyzing a quicksort program. I'm not aware of any generic method that would apply to all divide and conquer algorithms.

On a side note, an alternative to quicksort is a merge sort, but it uses more memory (double if sorting values, double the number of pointers if sorting an array of pointers to objects).

Previous threads on sorting. I included some test results in post #16 of the thread in the first link:

https://www.physicsforums.com/showthread.php?t=218369

https://www.physicsforums.com/showthread.php?t=181589
 
Pithikos said:
Some time ago I wrote an exam where I had to write a pseudocode to sort elemens in a list. I used a quicksort with the pivot in the middle and wrote that the algorithm has time complexity in best case O(n*logn) because it is a divide-and-conquer algorithm. That didn't satisfy my teacher and he noted to me that not all divide-and-conquer algorithms have n*logn.
So how would someone reason about this? (Taking quicksort as en example)

If you want to figure out the runtime complexity of an algorithm, the best way is to look at the execution nesting within the algorithm.

For example let's say you have two loops, both go from 1 to some variable n. The run-time is going to be O(n^2). Also note that run-time is typically "worst-case" unless otherwise specified.

You basically use nesting as a hint for creating another factor. Look at the bounds of each loop and use that information to create your complexity factor.

The same also applies if you use recursion. The same idea is applied, although its typically more complex and you have to keep track of more variables (think memory state) when doing the analysis.

So in a nutshell translate all loops/recursive calls with boundary information to nested factors and use factors to generate complexity bound.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 31 ·
2
Replies
31
Views
5K
  • · Replies 17 ·
Replies
17
Views
4K