Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Raport
La lucrarea de laborator la SDA nr 5
Varianta 12
Chiinu 2016
Laboratory work no.6
Topic: Empiric analyzing of sorting and searching algorithms
Work processing:
Short theory on laboratory work topic and used methods:
Priority Queue Sort is a whole family of sorting algorithms, which differs one from another
by the way that the priority queue is implemented in them. The most usual priority queue sorting
algorithm is Heap Sort. It is based on the fact that a heap structure can be either minheap or
maxheap, which means that in the root of the heap will be either the smallest, or the greatest value in
an array respectively. This algorithm is based on the fact that in order to construct a heap it is needed
n swaps and logn comparisons.
Mathematical analysis of heapsort shows that, on average, the algorithm takes O(nlogn)
comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is
rare.
header.h
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define AND &&
int main() {
//unsorted elements
//we are saving elements from index 1
//so, index 0 is set to -1
int arr[20], bigArr[20000];
fulfill(bigArr, 20000);
clock_gettime(CLOCK_REALTIME, &data1);
// Put your function/functions here
// heapSort(bigArr, 20000, minHeap);
inserSort(bigArr, 20000);
clock_gettime(CLOCK_REALTIME, &data2);
printf("%u seconds\n", data2.tv_sec - data1.tv_sec);
printf("%ld nanoseconds\n", data2.tv_nsec - data1.tv_nsec);
puts("______________________________________");
return 0;
}
void heapSort(int arr[], int size, void (*alg)(int arr[], int size))
{
//variables
int i, j, tmp, sorted[size];
sorted[0] = -1; //index 0 is not used so set to -1
//sort
for(i = size , j = 1; i >= 1; i--, j++) {
alg(arr, i);
sorted[j] = arr[1];
arr[1] = arr[i - 1]; //put last element to root node
}
if(tmp != i) {
val = arr[i];
arr[i] = arr[tmp];
arr[tmp] = val;
maxHeap(arr, size);
}
}
}
1 The results computed by the program are unusual, because as results show, the heap sort is almost
10x slower than insertion sort, which is strange.
2 During this laboratory work, we learned how to make an empiric analyzing of sorting and
searching algorithms.
3 While doing this laboratory work, practical skills in analyzing of sorting and searching algorithms
was trained.
Bibliography:
1 Hand notes from Computer Programming lessons taught by lector dr., conf. univ. Kulev Mihail,
UTM, Chiinu, 2016
2 https://en.wikipedia.org/wiki/Heap_sort
3 http://www.tutorialspoint.com/data_structures_algorithms/heap_sort_algorithm.htm