Sei sulla pagina 1di 7

Ministerul Educaiei Republicii Moldova

Universitatea Tehnic a Moldovei


Facultatea Calculatoare, Informatic i
Microelectronic
Filiera anglofon (FAF)

Raport
La lucrarea de laborator la SDA nr 5

Varianta 12

Efectuat de: studentul gr. FAF-151, Varacuta A.

Verificat de: Doctor Conf. Universitar, Kulev M.

Chiinu 2016
Laboratory work no.6
Topic: Empiric analyzing of sorting and searching algorithms

Objectives: developing practical skills in analyzing of sorting and searching algorithms


Condition of the problem: write a C program for empiric analyzing of your algorithm with next
menu:

1. Demonstrative array of n elements (n<=20)


2. Array with random variables for 3 big numbers of elements (10000<n1<n2=2*n1<n3=2*n2)
3. Sorted array in ascending order
4. Sorted array in descending order

Variant no.12: Priority queue sort

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.

The steps of sorting are:

1. Heapify the array.

2. Retrieve the smallest/biggest value which is the root.

3. Reconstruct the heap.

4. Repeat from 2 until theres no more elements in the array.

Code of the program in C language:

header.h
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define AND &&

void display(int arr[], int size);


void fulfill(int arr[], int size);

void inserSort(int arr[], int size);


void heapSort(int arr[], int size, void (*alg)(int*, int));
void minHeap(int arr[], int size);
void maxHeap(int arr[], int size);

void (*alg)(int arr[], int size);

int main() {
//unsorted elements
//we are saving elements from index 1
//so, index 0 is set to -1
int arr[20], bigArr[20000];

//size of the array


// int n = sizeof(arr)/sizeof(arr[0]);

fulfill(bigArr, 20000);

/* //output unsorted elements


display(arr, 20);
puts("______________________________________");*/

//sort the elements

struct timespec data1, data2;

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("______________________________________");

//display sorted elements


display(bigArr, 20000);
puts("______________________________________");

/* //sort the elements


heapSort(arr, 20, maxHeap);

//display sorted elements


display(arr, 20);
puts("______________________________________");*/

return 0;
}

void display(int arr[], int size) {


int i;
for(i = 1; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

void fulfill(int arr[], int size) {


int i;
arr[0] = -1;
for (i = 1; i < size; i++)
{
arr[i] = rand();
}
}

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
}

//set the result


for(i = 0; i < size; i++) {
arr[i] = sorted[i];
}
}

void minHeap(int arr[], int size) {


int i, left, right, tmp, val;

for(i = size / 2; i >= 1; i--) {

//taking 1 as the start index


//if parent node = i
//so left child = 2i
//and right child = 2i+1
tmp = i;
left = (2 * i);
right = (2 * i) + 1;

if(left < size && arr[left] < arr[tmp]) {


tmp = left;
}

if(right < size && arr[right] < arr[tmp]) {


tmp = right;
}
if(tmp != i) {
val = arr[i];
arr[i] = arr[tmp];
arr[tmp] = val;
minHeap(arr, size);
}
}
}

void maxHeap(int arr[], int size) {


int i, left, right, tmp, val;

for(i = size / 2; i >= 1; i--) {

//taking 1 as the start index


//if parent node = i
//so left child = 2i
//and right child = 2i+1
tmp = i;
left = (2 * i);
right = (2 * i) + 1;

if(left < size && arr[left] > arr[tmp]) {


tmp = left;
}

if(right < size && arr[right] > arr[tmp]) {


tmp = right;
}

if(tmp != i) {
val = arr[i];
arr[i] = arr[tmp];
arr[tmp] = val;
maxHeap(arr, size);
}
}
}

void inserSort(int arr[], int size) {


int i, j, temp;
for (i = 0; i < size; i++) {
for (j = i; j > 0 AND arr[j - 1] > arr[j]; j--) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
Results:
Heap Sort time for 20000 random elements: 14 seconds 86445300 nanoseconds
Insertion Sort for 20000 random elements: 1 seconds -141224600 nanoseconds
Analysis of results and conclusions:

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

Potrebbero piacerti anche