Program-01
Program-01
Subject Code:21CS42
Program-01
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.
• The selection sort enhances the bubble sort by making only a single swap for
each pass through the rundown.
• In order to do this, a selection sort searches for the biggest value as it makes
a pass and, after finishing the pass, places it in the best possible area.
ALGORITHM
1. k ← length [A]
2. for j ←1 to n-1
3. smallest ← j
4. for I ← j + 1 to k
5. if A [i] < A [ smallest]
6. then smallest ← i
7. exchange (A [j], A [smallest])
1. In the selection sort, first of all, we set the initial element as a minimum.
2. Now we will compare the minimum with the second element. If the second element
turns out to be smaller than the minimum, we will swap them, followed by
assigning to a minimum to the third element.
3. Else if the second element is greater than the minimum, which is our first element,
then we will do nothing and move on to the third element and then compare it
with the minimum.
4. We will repeat this process until we reach the last element.
5. After the completion of each iteration, we will notice that our minimum has
reached the start of the unsorted list.
6. For each iteration, we will start the indexing from the first element of the unsorted
list. We will repeat the Steps from 1 to 4 until the list gets sorted or all the elements
get correctly positioned.
7. Consider the following example of an unsorted array that we will sort with the help
of the Selection Sort algorithm.
Example:
Consider the following depicted array as an example.
For the first position in the sorted list, the whole list is scanned sequentially. The first
position where 14 is stored presently, we search the whole list and find that 10 is the
lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum
value in the list, appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a
linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second
place. We swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array −
Logic: If we are given n elements, then in the first pass, it will do n-1 comparisons; in the
second pass, it will do n-2; in the third pass, it will do n-3 and so on. Thus, the total
number of comparisons can be found by;
Therefore, the selection sort algorithm encompasses a time complexity of O(n2) and a
space complexity of O(1) because it necessitates some extra memory space for temp
variable for swapping.
Time Complexities:
o Best Case Complexity: The selection sort algorithm has a best-case time complexity
of O(n2) for the already sorted array.
o Average Case Complexity: The average-case time complexity for the selection sort
algorithm is O(n2), in which the existing elements are in jumbled ordered, i.e., neither in
the ascending order nor in the descending order.
o Worst Case Complexity: The worst-case time complexity is also O(n2), which occurs when
we sort the descending order of an array into the ascending order.
In the selection sort algorithm, the time complexity is O(n2) in all three cases. This is
because, in each step, we are required to find minimum elements so that it can be placed
in the correct position. Once we trace the complete array, we will get our minimum
element.
PROGRAM
package DAA_Programs;
import java.util.Random;
import java.util.Scanner;
start_time = System.nanoTime();
selectionSort(a, n);
end_time = System.nanoTime();
System.out.println("\nThe elements after sorting");
for (int i = 0; i < n; i++) {
System.out.println("" + a[i]);
}
System.out.println("\nThe time required for sorting " + n + " numbers is: " +
(end_time - start_time) + " ns");
}
}
}
}
OUTPUT