Sorting Algorithms and Their Run Times

In summary, the conversation is about a student struggling to understand the pattern and formula for the run times of sorting algorithms. They are having trouble with their testing method and are asking for help in analyzing their algorithm to determine its big-O run time. They also mention that they have read about the run time being quadratic, but are unsure how to calculate it. The expert advises them to take the worst case time of the algorithm by counting operations and adding/multiplying them together to determine the big-O run time.
  • #1
wisvuze
372
1

Homework Statement



hi, I was asked to notice a pattern involving the run times of sorting algorithms.. however, the run time timer program I have keeps giving me different values for the same length lists :| will the pattern/formula be an approximation then? also, I read that the run time should follow n^2 or some kind of quadratic relation.. I just can't see it, the runtime values I'm getting are no way quadratic. could anybody help me?

thanks

Homework Equations


The Attempt at a Solution



here is my selection sort code , it is written in python:

Code:
def swapper(L, i):
    larger = i
    for e in range(i+1 , len(L)): 
        if L[e] > L[larger]:
            larger = e
            
    return larger    

def non_ascending(L):
    i = 0
    while i != len(L)-1:
        larger = swapper(L,i)
        L[i], L[larger] = L[larger], L[i]
        i += 1
 
Physics news on Phys.org
  • #2
It's difficult to test because of things like caches.
You either need to do the run 100s of times an take an average
or you should analyse the algorithm to decide the big-O
 
  • #3
mgb_phys said:
It's difficult to test because of things like caches.
You either need to do the run 100s of times an take an average
or you should analyse the algorithm to decide the big-O

ah, thanks for the speedy reply. I was certain that something was off with my testing method. could you show me how (briefly at least?) I could analyze my algorithm to decide the big-O?

thanks a lot :)
 
  • #4
wisvuze said:
also, I read that the run time should follow n^2 or some kind of quadratic relation..


That's its bigO run time, which you get by taking the worst case time of your algorithm. The worst case time is when you have N (N>1) values, how long will it take to sort? This is figured out by counting your operations and adding them/multiplying them together.

Example:
Code:
while(i<N): #done N times
  k[i] = k[o] #assigned N times
the O(n) = n^2 because n assignemnts in n loops = n(n)=n*2

if your code is
Code:
while(i<N): # done N times
  k[i] = k[o] #N times
  l[i] = l[o] # N times
the O(n) = n^2 because n+n assignments in n loops = n(2n)=2n*2, but 2 (as are all constants) is negligible so O(n) = n^2
 
Last edited:

1. What is a sorting algorithm?

A sorting algorithm is a step-by-step procedure used to rearrange a list of data items into a specific order.

2. How is the run time of a sorting algorithm measured?

The run time of a sorting algorithm is typically measured by the number of comparisons or swaps performed during the sorting process.

3. What is the best case run time for a sorting algorithm?

The best case run time for a sorting algorithm is when the input data is already in the desired order, resulting in a time complexity of O(n).

4. What is the worst case run time for a sorting algorithm?

The worst case run time for a sorting algorithm is when the input data is in reverse order, resulting in a time complexity of O(n^2) for most algorithms.

5. How do different sorting algorithms compare in terms of run time?

Different sorting algorithms have varying run times, with some being more efficient than others. For example, quicksort and mergesort have an average time complexity of O(n log n) while bubble sort has an average time complexity of O(n^2).

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
526
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Replies
2
Views
468
  • Engineering and Comp Sci Homework Help
Replies
6
Views
850
  • Programming and Computer Science
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top