AOA-Lab Exp1
AOA-Lab Exp1
AIM: To generate some random datasets. Thereafter, sort them using Insertion and
Selection sort and compare the sorting time used by each algorithm.
THEORY:
1. Selection Sort: This algorithm sorts the given array by finding the minimum
element and putting it in the beginning by swapping it. The array is divided into
two subarrays, these are sorted and unsorted arrays. Initially we consider sorted
subarray is empty. After each step, the minimum number is moved to the sorted
subarray from the unsorted subarray. This goes on until the array is completely
sorted.
Complexity: Time complexity is always O(N2).
Space complexity: O(1)
Algorithm:
i. Set minimum number, MIN at a[0]
ii. Search a[i] to a[n] to find the minimum element
iii. Swap the value at a[0]
iv. Increment the value, MIN by 1- to next element
v. Repeat till the array is sorted
2. Insertion Sort: The algorithm also creates 2 subarrays- sorted and unsorted.
The values from the unsorted subarray are picked and placed at the correct
position in sorted subarray after comparing it with the already present elements in
the sorted part.
Complexity: Time complexity is O(N2) in the worst case, i.e., sorting elements
arranged in descending order to ascending.
O(N) in the best case- when elements are already in ascending order,
Space complexity: O(1)
Algorithm:
i. Go from arr[1] to arr[n] of the array
ii. Compare the current element to the one preceding it.
iii. If key element is less than the preceding one, compare with the elements
before it. Move the elements greater in number one position ahead.
iv. Repeat till the array is sorted.
PROGRAM CODE:
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
//creating files
char
filename[5][15]={"numbers_1.dat","numbers_2.dat","numbers_3.dat","numbers_4.dat","numbers_
5.dat"};
min = i;
min = j;
if(min!=i)
temp=a[min];
a[min]=a[i];
a[i]=temp;
int i, key, j;
j = i - 1;
while (j >= 0 && arr[j] > key)//to compare till arr[0] and value is greater than temp
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main()
int i,temp,a;
int random_number;
int arr[LIMIT];
FILE *file;
float diff_sel[5];
float diff_ins[5];
//for loop for generating numbers, selection sort and insertion sort for 5 files
for (a=0;a<5;a++)
//to generate random numbers- file is opened, srand is used as seed with reference to time(0)
file=fopen(filename[a],"w");
srand((unsigned)time(0));
i=0;
do
i++;
}while (i<LIMIT);
fclose(file);
file=fopen(filename[a],"r");
i=0;
do
i++;
}while (i<LIMIT);
fclose(file);
gettimeofday(&start, NULL);
insertion_sort(arr,i);
gettimeofday(&stop, NULL);
printf("-----------------------------------------------------------\n");
printf("%s\t\t%0.2f\t\t ",filename[a],diff_ins[a]);
file=fopen(filename[a],"r");
i=0;
do
i++;
}while (i<LIMIT);
fclose(file);
gettimeofday(&start, NULL);
selection_sort(arr,i);
gettimeofday(&stop, NULL);
diff_sel[a]=stop.tv_sec-start.tv_sec;
printf("%0.2f\n",diff_sel[a]);
printf("-----------------------------------------------------------\n");
return 0;
}
OUTPUT:
CONCLUSION: We have learnt to create files and generate random numbers in them.
Also, the use of specific functions to generate numbers, to find difference in the sorting time
was learnt through this experiment.