Download SORTING ALGORITHM SELECTION SORT // Selection Sort Method

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
SORTING ALGORITHM
SELECTION SORT
// Selection Sort Method for Descending Order
public static void SelectionSort ( int [ ] num )
{
int i, j, first, temp;
for ( i = num.length – 1; i > 0; i – - )
{
first = 0; //initialize to subscript of first element
for(j = 1; j <= i; j ++) //locate smallest element between positions 1 and i.
{
if( num[ j ] < num[ first ] )
first = j;
}
temp = num[ first ]; //swap smallest found with element in position i.
num[ first ] = num[ i ];
num[ i ] = temp;
}
}
Sample
public class Sort {
static void selectionSort ( int [ ] array ) {
int locationofSmallest;
int temp;
for ( int step = 0; step < array.length-1; step++ ) {
// find the location of the smallest element from array[step] to
// the end of the array
locationofSmallest = step;
for ( int loc = step; loc < array.length; loc++ ) {
if ( array[loc] < array[locationofSmallest] ) {
locationofSmallest = loc;
}
}
// exchange array[step] with array[locationofSmallest]
temp = array[step];
array[step] = array[locationofSmallest];
array[locationofSmallest] = temp;
System.out.print(“\nPass ” + (step+1) + ” : “);
SelectionSort.display(array);
}
}
}
class SelectionSort {
static void display ( int array[] ) {
for ( int i=0; i< array.length; i++ )
System.out.print(array[i] + ” ” );
}
public static void main ( String args[] ) {
int arr[] = { 50, 30, 20, 10, 40 };
System.out.print(“Initial array : “);
display(arr);
Sort.selectionSort (arr);
}
}
User Input
import java.util.Scanner;
public class SelectionSort
{
public void SelectionSort(int[] arr){
for(int i=0; i<arr.length; i++)
{
for(int j=i+1; j<arr.length; j++)
{
if(arr[i] > arr[j] )
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + ” “);
}
}
//main class
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter the size of the array: “);
int n = input.nextInt();
int[] x = new int[n];
System.out.print(“Enter “+ n +” numbers: “);
for(int i=0; i<n; i++)
{
x[i] = input.nextInt();
}
SelectionSort access = new SelectionSort();
System.out.print(“The Sorted numbers: “);
access.SelectionSort(x);
}
}
BUBBLE SORT
// Bubble Sort Method for Descending Order
public static void BubbleSort( int [ ] num )
{
int j;
boolean flag = true; // set flag to true to begin first pass
int temp; //holding variable
while ( flag )
{
flag= false; //set flag to false awaiting a possible swap
for( j=0; j < num.length -1; j++ )
{
if ( num[ j ] < num[j+1] ) // change to > for ascending sort
{
temp = num[ j ];
//swap elements
num[ j ] = num[ j+1 ];
num[ j+1 ] = temp;
flag = true;
//shows a swap occurred
}
}
}
Sample
public class Sort {
public static void bubbleSort ( int [ ] array ) {
int temp;
for ( int pass = 1; pass < array.length; pass++ ) {
for ( int i = 0; i < array.length – 1; i++ ) {
if ( array[i] > array[i+1] ) {
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
System.out.print(“\nPass ” + (pass) + ” : “);
BubbleSort.display(array);
}
}
}
class BubbleSort {
static void display ( int array[] ) {
for ( int i=0; i< array.length; i++ )
System.out.print(array[i] + ” ” );
}
public static void main ( String args[] ) {
int arr[] = { 50, 30, 20, 10, 40 };
System.out.print(“Initial array : “);
display(arr);
Sort.bubbleSort (arr);
}
}
User Input
public class Sort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println(“Input size of integers to sort”);
n = in.nextInt();
int array[] = new int[n];
System.out.println(“Enter ” + n + ” integers”);
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n – 1 ); c++) {
for (d = 0; d < n – c – 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println(“Sorted list of numbers”);
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
INSERTION SORT
// Insertion Sort Method for Descending Order
public static void InsertionSort( int [ ] num)
{
int j;
// the number of items sorted so far
int key;
// the item to be inserted
int i;
for (j = 1; j < num.length; j++)
{
// Start with 1 (not 0)
key = num[ j ];
for(i = j – 1; (i >= 0) && (num[ i ] < key); i–) // Smaller values are moving up
{
num[ i+1 ] = num[ i ];
}
num[ i+1 ] = key; // Put the key in its proper location
}
}
Sample
class Sort {
static void Insertion ( Comparable []array, int n ) {
for ( int unsorted=1; unsorted<n; unsorted++ ) {
Comparable nextItem = array[unsorted];
int loc = unsorted;
while ( (loc>0) && ( array[loc-1].compareTo(nextItem)>0)) {
array[loc] = array[loc-1];
loc–;
}
array[loc] = nextItem;
System.out.print(“\nPass ” + unsorted + ” : “);
InsertionSort.display(array);
}
}
}
class InsertionSort {
static void display ( Comparable array[] ) {
for ( int i=0; i< array.length; i++ )
System.out.print(array[i] + ” ” );
}
public static void main ( String args[] ) {
Comparable arr[] = { new Integer(50), new Integer(30),
new Integer(20), new Integer(10),
new Integer(40) };
System.out.print(“Initial array : “);
display(arr);
Sort.Insertion (arr, arr.length );
}
}
Another Example
public class InsertionSort {
public static void main(String[] args) {
int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
insertionSort(input);
}
private static void printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + “, “);
}
System.out.println(“\n”);
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i–;
}
array[i+1] = key;
printNumbers(array);
}
}
}
User Input
import java.util.*;
class InsertionSort
{
static Scanner in = new Scanner(System.in);
public static void main(String[]args)
{
System.out.println(“Enter your Array Size”);
int n = in.nextInt();
int[]arr = new int[n];
fillArray(arr,n);
insertionSort(arr,n);// callint selection sort method
printArray(arr,n);
}
// Insertion sort method
static void insertionSort(int[]a,int n)
{
for(int out=1;out<n;out++)
{
int temp = a[out]; // 2 1 5 3 0
int in = out;
while(in>0&&a[in-1]>=temp)
{
a[in]=a[in-1];
in–;
}
a[in]=temp;
}
}
static void fillArray(int []a, int n)// fill array Method
{
System.out.println(“Enter Your Elements”);
for(int i=0;i<n;i++)
a[i]=in.nextInt();
}
static void printArray(int []a, int n)// print array Method.
{
System.out.print(“Array Elements = “);
for(int i=0;i<n;i++)
System.out.print(a[i]+”\t”);
System.out.println(” “);
}
}
MERGE SORT
Sample
public class MergeSort {
int array[];
int size;
public MergeSort(int n) {
size=n;
//create array with size n
array=new int[n];
//asign value into the array
for (int i=0;i<n;i++){
array[i]=(int) Math.round(Math.random()*89+10);
}
}
public int getSize() {
return size;
}
public void merge(int left, int mid, int right) {
int temp [] =new int[right-left+1];
int i = left;
int j = mid+1;
int k = 0;
while (i <= mid && j <= right) {
if (array[i] <= array[j]) {
temp[k] = array[i];
k++;
i++;
} else { //array[i]>array[j]
temp[k] = array[j];
k++;
j++;
}
}
while(j<=right) temp[k++]=array[j++];
while(i<=mid) temp[k++]=array[i++];
for(k=0;k<temp.length;k++) array[left+k]=temp[k];
}
public void merge_sort(int left,int right){
// Check if low is smaller then high, if not then the array is sorted
if(left<right){
// Get the index of the element which is in the middle
int mid=(left+right)/2;
// Sort the left side of the array
merge_sort(left,mid);
// Sort the right side of the array
merge_sort(mid+1,right);
// Combine them both
merge(left,mid,right);
}
}
public void print(){
System.out.println(“Contents of the Array”);
for(int k=0;k<15;k++) {
System.out.print(array[k]+” | “);
}
System.out.println();
}
public static void main(String args[]){
MergeSort m=new MergeSort(15);
System.out.println(“Before Sort <<<<<<<<<<<<<<<<<<<<<”);
m.print();
m.merge_sort(0,m.getSize()-1);
System.out.println(“After Sort > > > > > > > > > > > >”);
m.print();
System.out.println(“=======+============+=======+============+=========”);
MergeSort m2=new MergeSort(25);
System.out.println(“Before Sort <<<<<<<<<<<<<<<<<<<<<”);
m2.print();
m2.merge_sort(0,m2.getSize()-1);
System.out.println(“After Sort > > > > > > > > > > > >”);
m2.print();
System.out.println(“=======+============+=======+============+=========”);
MergeSort m3=new MergeSort(30);
System.out.println(“Before Sort <<<<<<<<<<<<<<<<<<<<<”);
m3.print();
m3.merge_sort(0,m3.getSize()-1);
System.out.println(“After Sort > > > > > > > > > > > >”);
m3.print();
System.out.println(“=======+============+=======+============+=========”);
}
}
Related documents