Thursday, May 25, 2017

Program in java to delete an element in an unsorted array

/*
 * 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;
import java.util.Scanner;
/**
 *
 * @author arpis
 */
public class deleteinsorted {
     public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
        System.out.println(" enter the number of elements in your array");
        int i =sc.nextInt();
        int ar[] = new int[i];
        // taking the input of the arrays
        System.out.println("enter the elements of the arrays");
        for(int j=0;j<(i);j++){
            ar[j]=sc.nextInt();
        }
          int l= i-1;
         System.out.println("enter the number to be deleted");
         int delnum=sc.nextInt();
         for(int j=0;j<(i);j++){
            if(ar[j]==delnum){
              ar[j]=(ar[j]-ar[j]);
             
                if(j==l){
                    System.out.println("new array with the deleted element is");
                    for(int ii=0;ii<(l);ii++){
                           System.out.println(ar[ii]);
        }
                }
                else{
                    for(int k=ar[j];k<l;k++){
                        ar[k]=ar[k+1];
                       
                    }
                    System.out.println("new array with the deleted element is");
                      for(int iii=0;iii<(l);iii++){
                           System.out.println(ar[iii]);
                }
               
            }
        }
        
}}
}

Program in java to insert a number in an unsorted array at any position

/*
 * 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 insunsordedarray {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
        System.out.println(" enter the number of elements in your array");
        int i =sc.nextInt();
        int ar[] = new int[i+10];
        // taking the input of the arrays
        System.out.println("enter the elements of the arrays");
        for(int j=0;j<(i);j++){
            ar[j]=sc.nextInt();
        }
       
       
       System.out.println("enter the posotion to update");
      int pos =sc.nextInt();
     
       System.out.println("enter the new number "+pos);
       int newnum = sc.nextInt();
        if(ar[(pos-1)]==0&&pos<ar.length){
            System.out.println("array replaced with"+newnum);
          ar[pos-1]=newnum;
      }
        else{
            System.out.println("this position exceeds the array size");
        }
        if(ar[(pos-1)]!=0&&pos<ar.length){
            for(int k=(i-1);k>=pos-1;k--){
               ar[k+1]=ar[k];
            }
             ar[pos-1]=newnum;
        }
         for(int j=0;j<(i+10);j++){
             System.out.println(ar[j]);
        }
       
    }
   
}

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]);
        }
    }
}

program in java to search for a number in an unsorted array

/*
 * 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;
class a{
    public int al(int i){
         System.out.println("number found");
         int l =i+1;
        System.out.println("found at "+l);
       
         return i;
    }
}
public class searchunsortedarray{
    public static void main(String[] args) {
        int i = 0;
        boolean found= false;
        System.out.println("enter the limit of your array");
        Scanner sc = new Scanner(System.in);
      
         i = sc.nextInt();
        int a[] = new int[i];
        System.out.println("enter the elemets in your array");
        for (int j = 0; j < a.length; j++) {
            a[j] = sc.nextInt();
        }
 a sa= new a();
        System.out.println("enter the element you want to search for");
        int key;
        key = sc.nextInt();
        for (int j = 0; j < a.length; j++) {
                if(a[j]==key){
                    found= true;
                  sa.al(j);
                   
                }
                else{
                     found= false;
                }
        }
        if(found!=true){
         
            System.out.println("sorry! element not found");
           
           
           
        }
    }
}