GeekInterview.com
Series: Subject: Topic:
Question: 44 of 51

What is the difference in Searching and Sorting ?

Asked by: Interview Candidate | Asked on: Oct 3rd, 2007
Showing Answers 1 - 5 of 5 Answers
saravnan

Answered On : Nov 21st, 2007

Searching means search the information in a common pool where as sorting means arrange the content in any order.

  
Login to rate this answer.

Searching is to search the element in the given list, whereas Sorting is to arrange the given list in a particular order ( ascending or descending order )

  
Login to rate this answer.
hashini

Answered On : Sep 4th, 2008

View all answers by hashini

Searchingmeans finding whether the element is present in the listor not whereas sorting means Arranging the list in ascending or descending order.

  
Login to rate this answer.
kbjarnason

Answered On : Jul 2nd, 2010

View all answers by kbjarnason

Searching is finding things, sorting is arranging them.

Suppose you have the characters 'Z', 'A' and 'M' in an array.  Your code needs to know if 'M' is in the array.  How does it do it?  It searches.  "Is element 0 an 'M'?  No, it's a 'Z'.  Is element 1 an 'M'?  No, it's an 'A'.  Is element 2 an 'M'?  Yes, it is.  M is in the array."

Of course, the characters of the array are not in order.  To put them in order, you would sort them, perhaps as follows:

   Is 'Z' larger than 'A'?  Yes, so exchange them:  'A', 'Z', 'M'
   Is 'Z' larger than 'M'?  Yes, so exchange them: 'A', 'M', 'Z'
   Is 'Z' larger than... oops, we're at the end.  Done sorting.

Sorrting and searching are very distinct operations, but sorting can actually help searching.  For example, suppose your array had all 26 letters of the alphabet, but in random order, and your task was to find out the index of letters as someone passed them to you.

Suppose they pass an 'A'.  It might be in the first spot, but chances are against it.  Same for the second spot.  And so on.  As you examine more spots, the chances of finding the 'A' go up, until you have examined _every_ spot, all 26 of them, and your chances of finding it are now 100%.

Of course, you may have found it sooner, too.  Obviously, _some_ letter is in the first spot, another in the second, and so on, meaning if someone asks you those letters, you finish searching for them very quickly... but there's also some letter in the last spot and the second last spot, and those take longer.

If you average it out, searching this way takes about 13 tries per character - half of the number of elements being examined.  Could you improve that?

Absolutely: sort the array.  Get the desired character, say an 'R'.  Now start halfway into the array, where you get, say, an M.

Is R > M?  Yes; so you want to move "forwards".  How much?  You started with a jump of 13, you want half of that.  Call it 6.  Move ahead 6 elements, you get, oh, an S.  Is R > S?  No, so you jump back, again by half as much - 3 characters. 

If you do this, you'll discover that the average number of tries to find the desired letter is no longer 13 tries, but a little less than 5.  Indeed, you can do this with most any data set, and it doesn't have to be contiguous - it doesn't need to have _every_ letter or number in the set, it just needs to be sorted.  Start with the middle item, check it against the desired item, jump forwards or backwards by half as much as you started with, repeat.  If your "jump size" reaches 0 and you haven't found the item, it's not there.

Using this method, you can search for an element in a set of 1,024 items in just 10 steps instead of averaging 512 steps the other way, or in a set of 4.2 billion items in just 32 steps, instead of averaging 2.1 billion steps the other way.

Sorting and searching are distinct, but sorting can vastly help searching.

  
Login to rate this answer.
nsudheer

Answered On : Jun 4th, 2011

View all answers by nsudheer

Searching means Searching
Sorting means Sorting

Nothing special in its meaning, even in computer science. But, the algorithms to achieve this task are going to be special

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.