0% found this document useful (0 votes)
12 views6 pages

AOA-Lab Exp1

The document outlines an experiment aimed at generating random datasets and comparing the sorting times of Insertion Sort and Selection Sort algorithms. It provides a detailed explanation of both sorting algorithms, their complexities, and includes a program code that implements the sorting and measures the time taken for each. The conclusion highlights the learning outcomes related to file creation, random number generation, and timing functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

AOA-Lab Exp1

The document outlines an experiment aimed at generating random datasets and comparing the sorting times of Insertion Sort and Selection Sort algorithms. It provides a detailed explanation of both sorting algorithms, their complexities, and includes a program code that implements the sorting and measures the time taken for each. The conclusion highlights the learning outcomes related to file creation, random number generation, and timing functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT 1

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>

#define LIMIT 100000

//creating files

char
filename[5][15]={"numbers_1.dat","numbers_2.dat","numbers_3.dat","numbers_4.dat","numbers_
5.dat"};

void selection_sort(int a[],int n)

int i, j, min, temp;

//one by one traversing each number in array

for (i = 0; i < n-1; i++)

// Finding minimum number

min = i;

for (j = i+1; j < n; j++)

if (a[j] < a[min])

min = j;

//swap if minimum number is found

if(min!=i)

temp=a[min];

a[min]=a[i];
a[i]=temp;

void insertion_sort(int arr[], int n)

int i, key, j;

//to go through each element from arr[1] to arr[n]

for (i = 1; i < n; i++)

key = arr[i]; //storing the element as temp

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()

struct timeval stop, start;

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)

//random numbers are generated using rand function

file=fopen(filename[a],"w");

srand((unsigned)time(0));

i=0;

do

random_number = (int) (rand()%1000) ;

fprintf(file, "%d\n", random_number);

i++;

}while (i<LIMIT);

fclose(file);

file=fopen(filename[a],"r");

i=0;

do

fscanf(file, "%d", &arr[i]);

i++;

}while (i<LIMIT);

fclose(file);

gettimeofday(&start, NULL);

insertion_sort(arr,i);

gettimeofday(&stop, NULL);

//to calculate the time taken in sorting


diff_ins[a]=stop.tv_sec-start.tv_sec;

//printing the result of insertion sort

printf("\n\nDATASET | INSERTION SORT | SELECTION SORT \n");

printf(" seconds seconds \n");

printf("-----------------------------------------------------------\n");

printf("%s\t\t%0.2f\t\t ",filename[a],diff_ins[a]);

file=fopen(filename[a],"r");

i=0;

do

fscanf(file, "%d", &arr[i]);

i++;

}while (i<LIMIT);

fclose(file);

gettimeofday(&start, NULL);

selection_sort(arr,i);

gettimeofday(&stop, NULL);

//to calculate the time taken in sorting

diff_sel[a]=stop.tv_sec-start.tv_sec;

//printing the result of selection sort

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.

You might also like