0% found this document useful (0 votes)
2 views

Program-01

The document outlines a program for sorting a set of integers using the Selection Sort algorithm, detailing its implementation in Java. It explains the algorithm's mechanics, time complexity (O(n²) for all cases), and provides a sample code that generates random integers for sorting. Additionally, it emphasizes the importance of recording the time taken for sorting as the number of elements increases beyond 5000.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program-01

The document outlines a program for sorting a set of integers using the Selection Sort algorithm, detailing its implementation in Java. It explains the algorithm's mechanics, time complexity (O(n²) for all cases), and provides a sample code that generates random integers for sorting. Additionally, it emphasizes the importance of recording the time taken for sorting as the number of elements increases beyond 5000.

Uploaded by

raviba1998
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Subject Code:21CS42

Subject :Design and Analysis of Algorithm

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.

Theory Concept : Selection Sort

• 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.

VTU 4TH SEM Page 1


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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

How Selection Sort works

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.

VTU 4TH SEM Page 2


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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.

VTU 4TH SEM Page 3


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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 −

VTU 4TH SEM Page 4


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

VTU 4TH SEM Page 5


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

Complexity Analysis of Selection Sort


Input: Given n input elements.

Output: Number of steps incurred to sort a list.

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.

VTU 4TH SEM Page 6


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

PROGRAM

package DAA_Programs;

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

public class SelectionSort {

public static int SIZE = 7000;

public static void main(String[] args) throws


ArrayIndexOutOfBoundsException {
int a[] = new int[SIZE];
System.out.println("Enter the total number of elements for sorting:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random m = new Random();
for (int i = 0; i < n; i++) {
a[i] = m.nextInt(10) + 1;
}
System.out.println("\nThe elements before sorting....");
for (int i = 0; i < n; i++) {
System.out.println("" + a[i]);
}
long start_time, end_time;

VTU 4TH SEM Page 7


21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

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");
}

static void selectionSort(int[] a, int n) {


int i, j, minValue = 0, minIndex = 0, temp;
for (i = 0; i < n; i++) {
minValue = a[i];
minIndex = i;
for (j = i; j < n; j++) {
if (a[j] < minValue) {
minValue = a[j];
minIndex = j;
}
}
if (minValue < a[i]) {
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
VTU 4TH SEM Page 8
21CS42 | DESIGN & ANALYSIS OF ALGORITHM | SEARCH CREATORS

}
}
}

OUTPUT

VTU 4TH SEM Page 9

You might also like