Sunday 3 December 2017

Array Sort

Sorting Arrays
  • Selection Sort
  • Bubble Sort
Before we go to sorting , learn how to swap two values.
let us take x=30 and y=20, now interchange these two in java.
we might write
        x=y;
        y=x;
Now the value of x will be 20 and y also will be 20, coz in the first line y is assigned to x.Therefore the value of x is 20.In the second line ,we assign 20 to y not 30.Therefore we need one more temporary variable.
       temp=x;
        x=y;
        y=temp;
temp takes the value 30, x becomes 20, and y becomes 30.
Bubble Sort:
import java.util.*;
class bubble{
public static void main()
   {
int a[]=new int[10];
Scanner s=new Scanner(System.in);
int i,j,l,temp; 
l=a.length ;
System.out.println("Enter the values");
for(i=0;i<l;i++) -----------------------------------1
a[i]=s.nextInt();-----------------------------------2

for(i=0;i< l-1;i++)--------------------------------3
{
    for(j=i+1;j<l;j++)------------------------------4
      {
         if(a[i]>a[j])----------------------------------5
           {
              temp=a[i];
              a[i]=a[j];
               a[j]=temp;
           }
     }
}
System.out.println("The values after sorting");
  for(i=0;i<l;i++)
System.out.println(a[i]);
}
}

step 1 and 2  : input values
step 3 and 4 : There should be 2 loops,so that we compare the top most element with the least value.
step 4: If the top value is bigger(in case you want to sort in ascending order) the you should change it.Therefore swap the values.
Selection Sort :
import java.util.*;
class selection
{
public static void main()
   {
int a[]=new int[10];
Scanner s=new Scanner(System.in);
int i,j,l,temp,loc; 
l=a.length ;
System.out.println("Enter the values");
for(i=0;i<l;i++) 

a[i]=s.nextInt();
for(i=0;i< l-1;i++)
{
      small=a[i];---------------------------------------1
      loc=i;---------------------------------------------2
    for(j=i+1;j<l;j++)
        {
            if(small > a[j])------------------------------3
              {
                 small=a[j];-------------------------------4
                 loc=j;
              }
        }
        temp=a[i];------------------------------------5
        a[i]=a[loc];
        a[loc]=temp;
    }
System.out.println("The values after sorting");
  for(i=0;i<l;i++)
System.out.println(a[i]);
}
}

step 1 : Take the value in ith place as the small value.Initially the first value is small.So that we can compare it with other values.
step 2: Record the location of that value
step 3 :Compare the small value with the other values
step 4 :If small is bigger than the other value then assign that value to small.
step 5: Swap the values
 Write a program to pass an array to a function:
import java.util.*;
class arr2
{
void get(int a[], int l)
{   int i;
    for(i=0;i<l;i++)  
    System.out.println(a[i]);
}
public static void main()
   {
arr2 ob=new arr2();

int a[]=new int[10];
Scanner s=new Scanner(System.in);
int i,j,l,temp; 
l=a.length ;
System.out.println("Enter the values");
for(i=0;i<l;i++)  

a[i]=s.nextInt();
ob.get(a,l);
}
}

No comments:

Post a Comment