Note that there are some explanatory texts on larger screens.

plurals
  1. POinsertion sort vs bubble sort vs quicksort algorithm
    text
    copied!<p>I am working on a research in the class I tested bubble sort and insertion sort and quick sort , i did the test on random numbers. The results shows that insertion sort is more quicker than bubble sort and quick sort is the most slower one.</p> <p>So I have the below ranking in terms of time</p> <ol> <li>insertion sort (the fastest)</li> <li>bubble sort (second score)</li> <li>quick sort (the slowest)</li> </ol> <p>Taking into account that insertion and bubble sort have a complexity of O(n2) while quick sort O(n log n) and O (n log n) should be faster !!!</p> <p>Could anybody share with me explanations?</p> <p>Thanks</p> <pre><code>(NSMutableArray *)quickSort:(NSMutableArray *)a { // Log the contents of the incoming array NSLog(@"%@", a); // Create two temporary storage lists NSMutableArray *listOne = [[[NSMutableArray alloc] initWithCapacity:[a count]] autorelease]; NSMutableArray *listTwo = [[[NSMutableArray alloc] initWithCapacity:[a count]] autorelease]; int pivot = 4; // Divide the incoming array at the pivot for (int i = 0; i &lt; [a count]; i++) { if ([[a objectAtIndex:i] intValue] &lt; pivot) { [listOne addObject:[a objectAtIndex:i]]; } else if ([[a objectAtIndex:i] intValue] &gt; pivot) { [listTwo addObject:[a objectAtIndex:i]]; } } // Sort each of the lesser and greater lists using a bubble sort listOne = [self bubbleSort:listOne]; listTwo = [self bubbleSort:listTwo]; // Merge pivot onto lesser list listOne addObject:[[NSNumber alloc] initWithInt:pivot]]; // Merge greater list onto lesser list for (int i = 0; i &lt; [listTwo count]; i++) { [listOne addObject:[listTwo objectAtIndex:i]]; } // Log the contents of the outgoing array NSLog(@"%@", listOne); // Return array return listOne; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload