My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Shell Sort
Posted Date: 15 Feb 2008 Resource Type: Articles/Knowledge Sharing Category: General
|
Posted By: rajasekhar Member Level: Gold Rating: Points: 4
|
|
|
|
Shell Sort
Algorithm Analysis
Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n2) class of sorting algorithms. Of course, the shell sort is also the most complex of the O(n2) algorithms.
The shell sort is a "diminishing increment sort", better known as a "comb sort" to the unwashed programming masses. The algorithm makes multiple passes through the list, and each time sorts a number of equally sized sets using the insertion sort. The size of the set to be sorted gets larger with each pass through the list, until the set consists of the entire list. (Note that as the size of the set increases, the number of sets to be sorted decreases.) This sets the insertion sort up for an almost-best case run each iteration with a complexity that approaches O(n).
The items contained in each set are not contiguous - rather, if there are i sets then a set is composed of every i-th element. For example, if there are 3 sets then the first set would contain the elements located at positions 1, 4, 7 and so on. The second set would contain the elements located at positions 2, 5, 8, and so on; while the third set would contain the items located at positions 3, 6, 9, and so on.
The size of the sets used for each iteration has a major impact on the efficiency of the sort. Several Heroes Of Computer ScienceTM, including Donald Knuth and Robert Sedgewick, have come up with more complicated versions of the shell sort that improve efficiency by carefully calculating the best sized sets to use for a given list.
Pros: Efficient for medium-size lists. Cons: Somewhat complex algorithm, not nearly as efficient as the merge, heap, and quick sorts.
Source Code
Below is the basic shell sort algorithm.
void shellSort(int numbers[], int array_size) { int i, j, increment, temp;
increment = 3; while (increment > 0) { for (i=0; i < array_size; i++) { j = i; temp = numbers[i]; while ((j >= increment) && (numbers[j-increment] > temp)) { numbers[j] = numbers[j - increment]; j = j - increment; } numbers[j] = temp; } if (increment/2 != 0) increment = increment/2; else if (increment == 1) increment = 0; else increment = 1; } }
|
Responses
|
| Author: Deepu 18 Mar 2008 | Member Level: Diamond Points : 1 | Hi Raj,Good work Keep going.
| | Author: shikha 18 Mar 2008 | Member Level: Diamond Points : 1 | Hello Rajshekhar,
I think you have worked really very much for this, it's nice.
Thanks Shikha
| | Author: rajasekhar 25 Mar 2008 | Member Level: Gold Points : 1 | Thank U
|
|
Watch TV Channels
|