0% found this document useful (0 votes)
27 views3 pages

DAA 2022-23 Manual

Uploaded by

Deepika
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)
27 views3 pages

DAA 2022-23 Manual

Uploaded by

Deepika
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/ 3

1.

Sort a given set of n integer elements using Selection Sort method and compute its time
complexity. Run the program for varied values of n> 5000 and record the time taken to
sort. Plot a graph of the time taken versus n. The elements can be read from a file or can
be generated using the random number generator. Demonstrate using C++/Java how the
brute force method works along with its time complexity analysis: worst case, average
case and best case.

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class SelectionSort


{

public static void selectionSort(int[] arr)


{
int n = arr.length;

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


{
int minIndex = i;

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


{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}

int temp = arr[i];


arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

public static void main(String[] args)


{
int[] inputArr;

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer value: ");


int num = scanner.nextInt();

inputArr = new int[num];

Random random = new Random();

for (int i = 0; i < num; i++)


{
inputArr[i] = random.nextInt(200);
}
long startTime = System.nanoTime();
selectionSort(inputArr);
long endTime = System.nanoTime();

long executionTime = endTime - startTime;


System.out.println("Sorted Array: " + Arrays.toString(inputArr));
System.out.println("Execution Time: " + executionTime + " nanoseconds");
}
}

Output:

Enter an integer value: 5000


Sorted Array: [0, 0… 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, ……. ……199, 199, 199]
Execution Time: 48836505 nanoseconds

Enter an integer value: 10000


Sorted Array: [0, 0… 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, ……. ……199, 199, 199]
Execution Time: 122102767 nanoseconds

Enter an integer value: 15000


Sorted Array: [0, 0… 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, ……. ……199, 199, 199]
Execution Time: 169015402 nanoseconds

Enter an integer value: 20000


Sorted Array: [0, 0… 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, ……. ……199, 199, 199]
Execution Time: 1016173601 nanoseconds

Number of Elements Time taken to sort


5000 21804849
10000 196836536
15000 484553361
20000 1078825727
Chart Title
Time taken to sort Number of Elements

1.2E+09
1E+09
800000000
600000000
400000000
200000000
0
1 2 3 4

Regarding the time complexity analysis of Selection Sort:

Worst Case: The worst case occurs when the input array is in reverse order. In
this case, selection sort performs n-1 comparisons for the first element, n-2 for
the second, and so on. This results in a total of (n-1) + (n-2) + ... + 1 = n(n-
1)/2 comparisons, which is in the order of O(n^2).

Average Case: The average case time complexity of selection sort is also
O(n^2). This is because, on average, the algorithm needs to perform
approximately the same number of comparisons and swaps as in the worst case.

Best Case: The best case occurs when the input array is already sorted. In this
case, selection sort still performs n-1 comparisons but makes only 0 or n-1
swaps. The number of swaps can be reduced by using an additional variable to
track the minimum element's index instead of swapping elements during each
iteration. The best case time complexity is still O(n^2) because the number of
comparisons remains the same.

Overall, selection sort has a time complexity of O(n^2) in all cases.

You might also like