Кіріспе
Салыстыруға негізделген сұрыптау алгоритмі
Компьютерлік ғылымда адаптивті үйірме сұрыптау – адаптивті сұрыптау отбасының салыстыруға негізделген сұрыптау алгоритмі. Бұл деректерде қазіргі тартіп болған кезде жақсы жұмыс істейтін үйірме сұрыптаудың түрі. 1992 жылы Христос Левкопулос пен Ола Петерсон жариялаған алгоритм тербелістер саны ретінде алдын ала сұрыпталудың жаңа өлшемін, Osc, пайдаланады. Дәстүрлі үйірме сұрыптау сияқты барлық деректерді үйірмеге орналастырудың орнына, адаптивті үйірме сұрыптау деректердің бір бөлігін ғана үйірмеге алады, сондықтан деректердің алдын ала сұрыпталуы жоғары болған кезде орындалу уақыты айтарлықтай қысқарады. Бұл келесі төрт қадамды қамтиды. Макс үйірме (Минимальды үйірме) құру: барлық деректерді үйірмеге орналастырыңыз, сондықтан барлық түйіндер оның әрбір дочер түйінінен үлкен немесе тең (Минимальды үйірме үшін кіші немесе тең) болады. Үйірменің бірінші элементін үйірменің соңғы элементімен ауыстырыңыз. Соңғы элементті тізімнен алып тастап, оны тізімнің соңына қойыңыз. Үйірмені бірінші элемент дұрыс орынға орналасуы үшін түзетіңіз. 2- және 3-қадамды үйірмеде бір ғана элемент қалғанша қайталаңыз. Соңғы элементті тізімнің соңына қойып, тізімді шығарыңыз. Тізімдегі деректер сұрыпталады. Төменде Макс үйірмені құратын және үйірме құрылғаннан кейін массивті сұрыптайтын C/C++ іске асырылуы келтірілген. /*
Массивті өсу ретімен сұрыптайтын C/C++ үлгілік үйірме сұрыптау коды
*/
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
// Макс үйірме екілік ағашын құратын функция
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{
if (child + 1 <= end) // екі дочер түйіні болған кезде
{
if (array[child + 1] > array[child])
{
child++; // үлкен дочер түйінін таңдау
}
}
if (array[parent] > array[child])
{
return; // егер ата-ана түйіні үлкен болса, онда ол қазірдің өзінде үйірмеленген
}
if (array[parent] < array[child]) // егер дочер түйіні ата-ана түйінінен үлкен болса
{
std::swap(array[parent], array[child]); // ата-ана мен дочер түйінін ауыстыру
parent = child;
child = child * 2 + 1; // циклды жалғастыру, дочер түйіні мен оның дочер түйіндерін салыстыру
}
}
}
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
// үйірме сұрыптау функциясы
void heapSort(int array[], int len)
{
for (int i = len / 2 - 1; i >= 0; i--) // 1-қадам: Макс үйірмені құру
{
heapify(array, i, len);
}
for (int i = len - 1; i >= 0; i--) // 4-қадам: 2- және 3-қадамды аяқталғанша қайталау
{
std::swap(array[0], array[i]); // 2-қадам: Макс элементті массив соңына орналастыру
heapify(array, 0, i); // 3-қадам: ағаштан Макс элементті алып тастап, қайтадан үйірмелеу
}
}
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
int main()
{
// сұрыпталатын массив
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int arrayLen = sizeof(array) / sizeof(*array); // массив ұзындығы
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
heapSort(array, arrayLen);
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
return 0;
}
In computer science, adaptive heap sort is a comparison based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Petersson in 1992, the algorithm utilizes a new measure of presortedness, Osc, as the number of oscillations. Instead of putting all the data into the heap as the traditional heap sort did, adaptive heap sort only take part of the data into the heap so that the run time will reduce significantly when the presortedness of the data is high. It usually involves the following four steps. Build a Max Heap(Min Heap): put all the data into the heap so that all nodes are either greater than or equal (less than or equal to for Min Heap) to each of its child nodes. Swap the first element of the heap with the last element of the heap. Remove the last element from the heap and put it at the end of the list. Adjust the heap so that the first element ends up at the right place in the heap. Repeat Step 2 and 3 until the heap has only one element. Put this last element at the end of the list and output the list. The data in the list will be sorted. Below is a C/C++ implementation that builds up a Max Heap and sorts the array after the heap is built. /*
A C/C++ sample heap sort code that sort an array to an increasing order
*/
// A function that build up a max heap binary tree
void heapify(int array[], int start, int end)
{
int parent = start;
int child = parent * 2 + 1;
while (child <= end)
{ if (child + 1 <= end) // when there are two child nodes
{
if (array[child + 1] > array[child])
{
child ++; //take the bigger child node
}
}
if (array[parent] > array[child])
{
return; //if the parent node is greater, then it's already heapified
}
if (array[parent] < array[child]) // when child node is greater than parent node
{
swap (array[parent], array[child]); // switch parent and child node
parent = child;
child = child * 2 + 1; //continue the loop, compare the child node and its child nodes
}
}
}
// heap sort function
void heap sort (int array[], int len)
{
for (int i = len/2 1; i >= 0; i ) //Step 1: build up the max heap
{
heapify(array, i, len);
}
for (int i = len 1; i >= 0; i ) //Step 4: repeat step 2 and 3 till finished
{
swap(array[0], array[i]); // Step 2: put the max at the end of the array
heapify (array, 0, i 1); // Step 3: remove the max from the tree and heapify again
}
}
int main
{
//the array that will be sorted
int array[] = {42, 1283, 123, 654, 239847, 45, 97, 85, 763, 90, 770, 616, 328, 1444, 911, 315, 38, 5040, 1};
int array len = sizeof(array)/sizeof(*array); //length of the array
heap sort (array, array len);
return 0;
}
Алдын ала сұрыпталудың өлшемдері
Алдын ала реттелгендік өлшемдері берілген тізбектегі қолданыстағы реттілікті бағалайды. Бұл алдын ала реттелгендік өлшемдері сұрыптау процесінде жиынға (heap) қосылатын деректер мөлшерін, сондай-ақ орындалу уақытының ең төменгі шегін анықтайды.
Кемшіліктері
Онжылдық зерттеулерге қарамастан, адаптивті үйме сұрыптау теориясы мен оның практикалық қолданылуы арасында әлі де айырмашылық бар. Алгоритм Картезиандық ағаштарды және көрсеткіштермен жұмыс істеуді қолданғандықтан, оның кэш пайдалану тиімділігі төмен және жадқа қажеттігі жоғары, бұл орындалу жылдамдығын нашарлатады.