Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added selection sort #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions JAVA/Selection Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.*;
class Main
{
static void sel_sort(int numArray[])
{
int n = numArray.length;

// traverse unsorted array
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (numArray[j] < numArray[min_idx])
min_idx = j;

// swap minimum element with compared element
int temp = numArray[min_idx];
numArray[min_idx] = numArray[i];
numArray[i] = temp;
}
}

public static void main(String args[])
{
//declare and print the original array
int numArray[] = {7,5,2,20,42,15,23,34,10};
System.out.println("Original Array:" + Arrays.toString(numArray));
//call selection sort routine
sel_sort(numArray);
//print the sorted array
System.out.println("Sorted Array:" + Arrays.toString(numArray));
}
}