0% found this document useful (0 votes)
3 views

DS Lab Programs

Its very useful for some one

Uploaded by

nadeem shaik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS Lab Programs

Its very useful for some one

Uploaded by

nadeem shaik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

One Dimentional Array:

import java.util.*;

class OneDim

public static void main(String[] args)

int i,n,search;

Scanner s=new Scanner(System.in);

System.out.println("enter the n value:");

n=s.nextInt();

int a[]=new int[n];

System.out.println("enter the array values");

for(i=0;i<n;i++)

a[i]=s.nextInt();

System.out.println("the array values");

for(i=0;i<n;i++)

System.out.println(“ “+a[i]);
}
2.Two Dimensional Array

import java.util.*;

class TwoDim

public static void main(String[] args)

int i,j,row,col;

Scanner s=new Scanner(System.in);

System.out.println("enter the row and col value:");

row=s.nextInt();

col=s.nextInt();

int a[][]=new int[row][col];

System.out.println("enter the matrix-A values");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

a[i][j]=s.nextInt();

}
}

System.out.println(" the matrix elements");

for(i=0;i<row;i++)

for(j=0;j<col;j++)

System.out.print(" "+a[i][j]);

System.out.println(" \n ");

}
3. Linear Search

import java.util.*;

class Search

public static void main(String[] args)

int i,n,search;

Scanner s=new Scanner(System.in);

System.out.println("enter the n value:");

n=s.nextInt();

int a[]=new int[n];

System.out.println("enter the array values");

for(i=0;i<n;i++)

a[i]=s.nextInt();

System.out.println("enter the searching elememt");


search=s.nextInt();

for(i=0;i<n;i++)

if(a[i]==search)

System.out.println(search+"is found at"+(i+1));

break;

if(i==n)

System.out.println(search+"is not found");

}
4.How to Insert a Array Element

import java.util.Scanner;

class InsertArray

public static void main(String[] args)

int n, pos, ele,i;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements are:");

for(i = 0; i < n; i++)

a[i] = s.nextInt();

System.out.print("Enter the position where you want to insert element:");

pos = s.nextInt();

System.out.print("Enter the element you want to insert:");

ele = s.nextInt();

if(pos > n)

printf(“Invalid Input”);
}

else

for (i = n – 1; i >= pos – 1; i--)

arr[i+1] = arr[i];

arr[pos-1] = ele;

System.out.println("After Inserting:");

for(i = 0; i <=n; i++)

System.out.print(a[i]+" ");

}
5. How to Delete an Array Element:

import java.util.Scanner;

class DeleteArray

public static void main(String[] args)

int n, pos, x,i;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements are:");

for(i = 0; i < n; i++)

a[i] = s.nextInt();

System.out.print("Enter the position where you want to deleted:");

pos = s.nextInt();

if (pos >= n+1)

System.out.print("Deletion not possible.\n”);

}
else

for (i = pos – 1; i < n – 1; i++)

arr[i] = arr[i+1];

System.out.println("After Deleting:");

for(i = 0; i < n-1; i++)

System.out.print(a[i]+" ");

}
6.How to Update a Array Element

import java.util.Scanner;

class UpdateArray

public static void main(String[] args)

int n, pos, x,i;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in array:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter all the elements are:");

for(i = 0; i < n; i++)

a[i] = s.nextInt();

System.out.print("Enter the position where you want to update element:");

pos = s.nextInt();

System.out.print("Enter the element you want to update:");

x = s.nextInt();

for(i=0;i<n;i++)

{
if(i==pos)

a[i]=x;

System.out.println("After Updating:");

for(i = 0; i < n; i++)

System.out.print(a[i]+" ");

}
7. Bubble Sort

import java.util.Scanner;

class BubbleSort

public static void main(String[] args)

int n, i, j, temp;

Scanner s = new Scanner(System.in);

System.out.print("Enter the size of the Bubble Sort:");

n = s.nextInt();

int arr[] = new int[n];

System.out.println("Enter Bubble sort elements:");

for(i = 0; i < n; i++)

arr[i] = s.nextInt();

for(i = 0; i < n - 1; i++)

for(j = 0; j < n - i - 1; j++)

if(arr[j] > arr[j + 1])

temp = arr[j];
arr[j] = arr[j + 1];

arr[j + 1] = temp;

System.out.println(" Bubble sort elements are:");

for(i = 0; i < n; i++)

System.out.print( arr[i]+” “);

}
8. Insertion Sort

import java.util.Scanner;

class InsertionSort

public static void main(String[] args)

int i, n, j, temp, min, key;

Scanner s = new Scanner(System.in);

System.out.print("Enter the size of the Insertion Sort:");

n = s.nextInt();

int arr[] = new int[n];

System.out.println("Enter Insertion sort elements:");

for(i = 0; i < n; i++)

arr[i] = s.nextInt();

for (i = 1; i < n; i++)

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

arr[j + 1] = arr[j];
j = j - 1;

arr[j + 1] = key;

System.out.println("Enter Insertion sort elements:");

for(i = 0; i < n; i++)

System.out.println(arr[i]+” “);

}
9. Selection Sort

import java.util.Scanner;

class SelectionSort

public static void main(String[] args)

int i, n, j, temp, min, key;

Scanner s = new Scanner(System.in);

System.out.print("Enter the size of the Selection Sort:");

n = s.nextInt();

int arr[] = new int[n];

System.out.println("Enter Seleciton sort elements:");

for(i = 0; i < n; i++)

arr[i] = s.nextInt();

for (i = 0; i < n - 1; i++)

min = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min])

min = j;

temp = arr[min];
arr[min] = arr[i];

arr[i] = temp;

System.out.println("Selection sort elements are:");

for(i = 0; i < n; i++)

System.out.println(arr[i]+” “);

You might also like