answersLogoWhite

0


Best Answer

It's always much quicker (on average) to search a sorted array than it is to search an unsorted array. This is because you can start in the middle of the array. If the value you seek is there then you're done. If it is not there, but the value you seek is less than the value you found, then you immediately know it must be in the left half of the array, otherwise it must be in the right half. In other words you've reduced the number of items to be searched by 50%. If you repeat the process with this smaller subset you'll reduce the search to 25% of the original array, then 12.5%, and so on. Eventually you'll find the value you're looking for. The worst case for any search is when the value does not exist. But you'll know that as soon as the reduced subset has no elements.

We often use Big-O notation to determine how long we expect an operation to take. We know that we can access any element in an array in constant time, which is denoted O(1). We also know that we can compare values in constant time. So we're really only interested in the cumulative time it takes to search the array. For an array of n elements, the worst case involves inspecting every element, which is denoted O(n), which means O(1)*n, or n constant-time operations. For a large array of say, 1,000,000 elements, that's a lot of constant-time operations.

If we imagine an unsorted array, it will always take O(n) time to determine that a value does not exist. But if we sort the array and inspect each element in turn, it will only take O(n/2) time on average because we can stop searching as soon as we find a value that is greater than the one being sought. However, the worst case is still O(n) if the value being sought happens to be greater than any in the array. But if we start in the middle of a sorted array, the average and worst cases both drop to just O(log n).

Note that there will be some overhead in recalculating the subset boundaries, which is itself a constant-time operation, but this is only of concern when dealing with small arrays. It would, in fact, be much quicker to search small arrays one item at a time. For larger arrays, reducing the problem by 50% on each iteration means the overhead quickly becomes irrelevant, but it only works when the array is sorted. Hence new programmers tend to spend an inordinate amount of time studying sorting algorithms to ensure their programs operate as efficiently as possible.

Some algorithms are better than others, but there is no single algorithm that is suitable in every situation. For instance, insert sort is the ideal sorting algorithm for small subsets, but when dealing with a subset of more than a few hundred items it is woefully inadequate. And while quicksort performs extremely well when dealing with large amounts of data, it is unstable (equal items may not be in the same order they were input) and it doesn't work at all when the amount of data is so enormous that it simply will not fit into working memory. For that you need merge sort which uses multiple tape drives or separately controlled disks to sort the data. With slow-to-access media such as this, efficiency is far more important than raw speed alone.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of sorting c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Advantages of manipulator in c plus plus?

theriyadhu


What are the advantages and disadvantages of sorting algorithm?

shell sort merits and demerits


What are the Advantages of c over c plus plus?

There are no advantages of C over C++ as such. Everything you can do in C you can also do in C++. However, by taking advantage of C++ object oriented programming, generic programming and template meta programming as well as C-style coding, you can produce more efficient machine code far more easily and more quickly than with C alone.


Bubble sorting in c plus plus?

bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }


What are the various Advantages Disadvantages of different sorting algorithms?

This is a thesis of a student from Thapar University, by Ramesh Chand Pandey. It gives excellent explanations on different sorting algorithms.


Advantages of C over C plus plus and java?

C can be faster than C++ programs, and definitely faster than Java, since Java is primarily interpreted. C is also somewhat less rigid in definitions as well, not as tightly structured as either C++ or Java can be.


What are advantages and disadvantages of merging?

merge sort is the most efficient way of sorting the list of array.


What is mean by sorting in c?

arranging items in some ordered sequence,


Main advantage of 'c' against 'c plus plus ' langugae?

C does not have any major advantages over C++ because any C program can be compiled under C++ with relatively minor modification. However, the C compiler works a bit quicker than that of C++ since there is no need to cater for object-oriented programming in C.


What is the code of sorting out an given unsorted array in text file for c plus plus?

Sorting arrays (of any type) can be achieved with the C++ standard library std::sort function: std::array<int, 5> a {9, 3, 5, 1, 7}; // fixed-length array std::vector<int> b {9, 3, 5, 1, 7}; // variable length array int c[] = {9, 3, 5, 1, 7}; // C-style array std::sort (a.begin(), a.end()); std::sort (b.begin(), b.end()); std::sort (c, c+5);


What is b plus b plus b plus c plus c plus c plus c?

b+b+b+c+c+c+c =3b+4c


What are the types of sorting in c?

Any type you want to write. C does not provide sorting routines natively; you have to either use a library routine or write something. Some library implementations are based on quicksort or heapsort but, again, that is not a C (or C++) thing - it is a run-time library thing.