-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort2.java
More file actions
54 lines (52 loc) · 1.53 KB
/
SelectionSort2.java
File metadata and controls
54 lines (52 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Program: Selection Sort in Descending Order
// Topic: Sorting Algorithms (Arrays)
// Description: Reads an array of integers from user input and sorts it in descending order using the Selection Sort algorithm.
// In each iteration, the algorithm finds the maximum element in the unsorted part of the array and swaps it with the current position,
// then displays the sorted array.
package Sorting;
import java.util.*;
/**
*
* @author Bankra DQ
*/
public class SelectionSort2 //Descending Sort
{
public void sort(int ar[],int n)
{
int max;
for(int i=0;i<n;i++)
{
max = i;
for(int j=i+1;j<n;j++)
{
if(ar[max]<ar[j])
{
max=j;
}
}
int temp=ar[max];
ar[max]=ar[i];
ar[i]=temp;
}
System.out.println("Sorted Array :");
for(int i=0;i<n;i++)
{
System.out.println(ar[i]);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int size;
System.out.println("Enter Number oF Elements :");
size=sc.nextInt();
int ar[]=new int [size];
System.out.println("Enter The Elements Of the Array :");
for(int i=0;i<size;i++)
{
ar[i]=sc.nextInt();
}
SelectionSort2 obj=new SelectionSort2();
obj.sort(ar, size);
}
}