LAB2
LAB2
LAB2
REPORT
Laboratory work nr. 2
At Computer Programming
Performed by:
St.gr. FAF-233 Postoronca Dumitru
Checked by:
Dr., univ. conf. Furdui Alexandru
Chisinau – 2023
Laboratory work 2
Topic: Processing one-dimensional arrays in the C language
Purpose of the laboratory work: Studying the possibilities and means of language C for programming algorithms
with branched and cyclic structure when processing one-dimensional arrays
Problem condition [1] : For the given one-dimensional array consisting of n elements of type real: (Variant Hard:)
Implement these sorting algorithms: Bubble, Selection, Insertion, Quick Sort and explain them
Input data:
n – size of the array
arr[n] – introducing all elements of the array
Output data:
4 sorted arrays and time needed to sort each one
Program code (text) in C language (listing of the program):
#include<stdio.h>
#include<malloc.h>
#include<sys/time.h>
long ex_time(struct timeval begin, struct timeval end){
long seconds = (end.tv_sec-begin.tv_sec);
long micros = ((seconds*1000000)+end.tv_usec)-(begin.tv_usec);
return micros;
}
void swap(int *x, int *y){
int temp=*x;
*x=*y;
*y=temp;
}
void green(){
printf("\033[0;32m");
}
void reset(){
printf(" \033[0;37m");
}
void print_result(char *sort_type, int arr[], int n, struct timeval begin, struct timeval
end){
int size = strlen(sort_type);
green();
for (int i = 0; i < size; i++)
printf("%c", sort_type[i]);
printf(" sorted - Complexity -> O(N*N)\n");
reset();
for (int i = 0; i < n; i++)
printf("%i ", arr[i]);
green();
printf("\nExecution time is %d microseconds\n\n", ex_time(begin, end));
reset();
}
int bubble_sort(int or_arr[], int n){
struct timeval begin, end;
int arr[n];
for (int i = 0; i < n; i++)arr[i]=or_arr[i];
gettimeofday(&begin, NULL);
for (int i = 0; i < n; i++){
for (int j = 0; j < n-1; j++){
if (arr[j]>arr[j+1])
{
swap(&arr[j], &arr[j+1]);
}
}
}
gettimeofday(&end, NULL);
print_result("Bubble", arr, n, begin, end);
return 0;
}
int selection_sort(int or_arr[], int n){
struct timeval begin, end;
int arr[n];
for (int i = 0; i < n; i++)arr[i]=or_arr[i];
gettimeofday(&begin, NULL);
for (int i = 0; i < n-1; i++){
int iMin = i;
for (int j = i+1; j < n; j++){
if(arr[j]<arr[iMin])
iMin = j;
}
if (iMin!=i){
swap(&arr[i], &arr[iMin]);
}
}
gettimeofday(&end, NULL);
print_result("Selection", arr, n, begin, end);
return 0;
}
int insertion_sort(int or_arr[], int n){
struct timeval begin, end;
int arr[n];
for (int i = 0; i < n; i++)arr[i]=or_arr[i];
gettimeofday(&begin, NULL);
int j, temp;
for (int i = 1; i < n; i++){
j=i;
while (j>0 && arr[j]<arr[j-1]){
swap(&arr[j], &arr[j-1]);
j--;
}
}
gettimeofday(&end, NULL);
print_result("Insertion", arr, n, begin, end);
return 0;
}
int partition(int *arr, int low, int hight){
int piv_val = arr[hight];
int i = low;
for (int j = low; j < hight; j++){
if(piv_val>=arr[j]){
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[hight]);
return i;
}
int quicksort_recursion(int *arr,int low, int hight){
if (low<hight)
{
int piv_index = partition(arr, low, hight);
quicksort_recursion(arr, low, piv_index-1);
quicksort_recursion(arr, piv_index+1, hight);
}
}
int quick_sort(int or_arr[], int n){
struct timeval begin, end;
int arr[n];
for (int i = 0; i < n; i++)arr[i]=or_arr[i];
gettimeofday(&begin, NULL);
quicksort_recursion(arr, 0, n-1);
gettimeofday(&end, NULL);
print_result("Quick", arr, n, begin, end);
return 0;
}
int main(){
int *arr;int n;
printf("introduce the size of array: "); scanf("%i", &n);
arr=(int*)malloc( n * sizeof(int));
printf("introduce %i elements: ", n);
for (int i = 0; i < n; i++)scanf("%i", &arr[i]);
bubble_sort(arr, n);
selection_sort(arr, n);
insertion_sort(arr, n);
quick_sort(arr, n);
free(arr);
return 0;
}
Results of running and testing the program (screenshots) :
Analysis of results and conclusions:
After this laboratory work, I developed my skills to analyze and process one-dimensional arrays. Also, after all tasks I
learned about 4 algorithms to sort an array and how them work:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order. It has a time complexity of O(n^2) in the worst case. After each iteration
through the array, it sorts the biggest element at the end and all other elements remain unsorted.
Selection Sort works by repeatedly selecting the minimum element from the unsorted portion and swapping it with the
first unsorted element. It’s much faster the Bubble sort because it firstly finds the smallest element and just the swaps
it.
Insertion Sort builds the final sorted array one item at a time by repeatedly taking the next element and inserting it into
its correct position. Its time complexity is O(n^2) in the worst case. It’s much faster than bubble sort or selection sort,
because if an element is already in a correct position, the program has to do fewer swapping operations and
comparisons and go further in array.
Quick sort is the fastest way to sort the array, because after every time we choose, we pivot 2 halves of the array
around the pivot are make the presorted and at the same time we put the pivot in final right position. It has an average
complexity of O(n log(n)), but if we chose the wrong pivot or elements are sorted too chaotic, algorithm’s complexity
is O(n^2)
To see the difference in speed of each algorithm I implemented a clock using <sys/time.h> library. So in the results
you can see in microseconds the time needed to sort the array with 500 elements and another one with 800
elements(bigger one crashes the terminal - maybe because of buffer overflow).
Bibliography
1.Carcea L., Vlas S., Bobicev V. Informatics: Tasks for laboratory works. Chisinau: UTM, 2005. - 19 p.
2. The overview of Computer Programming course lessons for students (lecturer: associate professor M. Kulev).
Chisinau, UTM, FCIM, 2023
3. Information about how to measure execution time
4. Informational videos about: Bubble sort, Selection sort, Insertion sort, Quick sort
5. Integer arrays generator