Shell sort is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). Shell sort starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. (Wikipedia)
Javavoid shell_sort(int array[], int length)
{
for (int gap = length/2; gap > 0; gap /= 2)
{
for (int i = gap; i < length; i += 1)
{
int temp = array[i];
int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap)
array[j] = array[j - gap];
array[j] = temp;
}
}
}
Matlabpublic int[] shell_sort(int[] array) {
for (int gap = array.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < array.length; i += 1) {
int temp = array[i];
int j;
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
}
}
return array;
}
function array = shell_sort(array)Python
length = numel(array);
gap = floor(length/2);
while gap > 0
for i = gap+1:length
temp = array(i);
j = i;
while (j >= gap+1) && (array(j-gap) > temp)
array(j) = array(j-gap);
j = j - gap;
end
array(j) = temp;
end
gap = floor(gap/2);
end
end
import math
def shell_sort(array):
length = len(array)
gap = math.floor(length/2)
while gap > 0:
for i in range(gap,length):
temp = array[i]
j = i
while (j >= gap) and (array[j - gap] > temp):
array[j] = array[j-gap]
j = j - gap
array[j] = temp
gap = math.floor(gap/2)
This comment has been removed by the author.
ReplyDeleteVery well written
ReplyDeleteBest Data Science Training In Hyderabad
Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.data science course in Hyderabad
ReplyDelete
ReplyDeleteI see some amazingly important and kept up to length of your strength searching for in your on the site
business analytics course