Solved

Sorting Algorithm Analysis

Posted on 2003-11-14
2
294 Views
Last Modified: 2010-04-02
Im working on a program that utilizes many of the sorts and allows me to compare them.  However I ran into a situation I am curious about.

For selection sort, for both string and integer sorting, why would the number of comparisons be the same even though the sorting order is different?  Then this also poses another question, why would string sorting take more time?

I also just wanted to confirm some of my findings.  
Either merge-sort or quick sort is good for large input correct? Is there a best?
Selection / Insertion sort for small input?
Also why does the pivot point matter on quicksort? So less items have to be compared?
0
Comment
Question by:killer455
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Expert Comment

by:waelothman
ID: 9748521
1 -  selection sort is order (n^2)
this because it work as follow
 1 it select the min from 1 -> n and put it in the 1st place (n compareson)
 2 it select the min from 2 ->n  and put it in the 2nd place (n-1 compareson)
.....
 n it select the min from n ->n  and put it in the nth place (n-n compareson)
so always the iteration is n+(n-1)+(n-2)+...+0 = n/2*(n-1) ie n^2

mege sort and quik sort is order n*lg(n) and this is best order for sorting but complectaed so when we user small numbers we user Selection / Insertion since in this case n^2 and  n*lg(n)  will be very near

the most problem in quik sort is the pivot because the the chossing of the pivot may change the order to o(n^2)
for example if you use sorted array and you choose the pivot as 1st element
you then divide array to 2 parts 1 greater than pivot 2 less than or equal pivot (if the pivot divide the array to 2 equal size array you reach optimal) but since array sorted the 1st array will contain 1 element and the second contain (n-1) so it as you do nothing


0
 
LVL 11

Accepted Solution

by:
bcladd earned 50 total points
ID: 9748960
How does selection sort work? Think about that for just a second.

Selection sort runs two count controlled loops. One indexes from the first to the last element in the array. The second, inner loop, indexed from the first index to the end of the loop looking for the index of the smallest element in that range. After the inner loop finishes, the smallest element is swapped with the element indexed by the first index.

Do you see anything interesting in that description of selection sort? No matter which element is the smallest in the remaining area to check, one item is compared with the current smallest in each iteration of the inner loop. So the number of times keys are compared is actually CONSTANT for any given input size regardless of the type or ordering of the input.

Your code counts KEY COMPARISONS. That is, comparisons between whole values stored inside the array to be sorted. Thus comparisons conunts comparing two strings exactly the same as it counts comparing two integers. It is true that the assembly code for comparing two integers is one (or two if you count the branch) instructions and comparing strings may be many instructions (it is a loop even if it looks like "=="). So the TIME for comparing two keys depends on the type and (perhaps) value of the key but the number of key comparisons does not.

-bcl
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Header of docx file 17 145
Embarcadero WebBroker REST server endpoint parameters 2 117
visual C++ 1 43
Creating a Windows C++ program in visual studio 2015 5 32
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question