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

Selection Sort

The document describes a Java program that implements the selection sort algorithm to sort an array of random integers. It includes user input for the number of elements and outputs the sorted array along with the number of basic operations performed during sorting. Additionally, it provides examples of basic operations for different array sizes, suggesting a performance analysis through graphing.

Uploaded by

challalokesh4211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Selection Sort

The document describes a Java program that implements the selection sort algorithm to sort an array of random integers. It includes user input for the number of elements and outputs the sorted array along with the number of basic operations performed during sorting. Additionally, it provides examples of basic operations for different array sizes, suggesting a performance analysis through graphing.

Uploaded by

challalokesh4211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program-1b:

Design and implement a program in Java that uses simple technique to sort n
elements using selection sort.

import java.util.Random;
import java.util.Scanner;
public class SelectionSort {
static int sortcount=0;
public static void selSort(int[] a) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
sortcount++;
if (a[j] < a[min]) {
min = j;
}
}
int temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of element: ");
int n = s.nextInt();
int[] a = new int[n];
//use random class object to generate random values
Random r = new Random();
System.out.println("intput numbers");
for (int i = 0; i < n; i++) {
a[i] = r.nextInt(10000); // Random numbers between 0 and
9999
System.out.println(a[i] + " ");
}
selSort(a);
System.out.println("Sorted numbers are:");
for(int i=0;i<n;i++)
System.out.println(a[i]);
System.out.println("Number of basic operations for sorting is:
"+sortcount);
}
}

Output:
Enter the number of element:
5
intput numbers
5275
3209
802
6226
5501
Sorted numbers are:
802
3209
5275
5501
6226
Number of basic operations for sorting is: 10

n=500 No. of Basic Operations= 124750


n=600 No. of Basic Operations= 179700
n=700 No. of Basic Operations= 244650
n=800 No. of Basic Operations= 319600
n=900 No. of Basic Operations= 404550
n=1000 No. of Basic Operations= 499500
(Draw the graph)

You might also like