Thursday, May 25, 2017

Program in java to sort an array- linear search

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package datastrucarraya;
/**
 *
 * @author arpis
 */
import java.util.Scanner;
public class sortarray {
    public static void main(String[] args) {
        int i = 0;
        System.out.println("enter the size of the array");
        Scanner sc = new Scanner(System.in);
        i = sc.nextInt();
        int a[] = new int[i];
      
    
        System.out.println("enter the elements in the array");
        for (int j = 0; j < a.length; j++) {
            a[j] = sc.nextInt();
        }
        int las =0;
         las = a.length-1;
         for(int j =0;j<a.length;j++){
             for(int k=j+1; k<a.length;k++){
                 if(a[j]>a[k]){
                     int temp=0;
                     temp =a[j];
                     a[j]=a[k];
                     a[k]=temp;
                 }
             }
         }
        
        
         System.out.println("the sorted array is");
  for (int j = 0; j < i; j++) {
            System.out.println(a[j]);
        }
    }
}

No comments:

Post a Comment