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

Array Index or Key. An Array Is Stored Such That The Position of Each Element Can Be Computed From Its Index Tuple by

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Array Index or Key. An Array Is Stored Such That The Position of Each Element Can Be Computed From Its Index Tuple by

Uploaded by

creatorcoder268
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DSA : The term DSA stands for Data Structures and Algorithms, in the context of Computer Science.

A data structure is a storage that is used to store and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated efficiently.

Types of DSA :

 Array
 Queue
 Stack
 Linked List
 Hash Map
 Tree
 Graph

Operations : Searching , Sorting

Array : it is a data structure consisting of a collection of elements of same type , each identified by at least one
array index or key. An array is stored such that the position of each element can be computed from its index tuple by
a mathematical formula.

creating array syntax : datatype[] variable = {valu1 , value2, ….. , valueN};

accessing all array element :

for(int index = 0 ; index<array.length ; index++){

System.out.println(array[index]);

For user-input array :


import java.util.Scanner;

public class Array{

public static void main(String[] args){

Scanner in = new Scanner(System.in);

System.out.println(“Enter no.of array element : \n”);

Int n = in.nextInt();

Int[] a = new int[10];

Int i;

// add element inside array

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

a[i] = in.nextInt();

System.out.println(“Array elements : “);

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

System.out.print(a[i]); }

In.close();

}
Searching : it used to find a specific item or element within a collection of data.

Types :
 Linear Search
 Binary Search
Linear Search code :
import java.util.Scanner;

public class LinearSearch {

public static void main(String[] args) {

int c , n , s;

int[] a;

Scanner in = new Scanner(System.in);

System.out.println("Enter number of elements");

n = in.nextInt();

a = new int[n];

System.out.println("Enter those " + n + " elements");

for (c = 0; c < n; c++) {

a[c] = in.nextInt();

System.out.println("Enter value to find");

s = in.nextInt();

for (c = 0; c < n; c++) {

if (a[c] == s) {

System.out.println(s + " is present at location " + (c + 1) + ".");

break;

if (c == n) {

System.out.println(s + " isn't present in array."); }

}
Binary Search Syntax :

class Search {

int search(int[] a , int l , int r , int x){

if(r>=l){

int mid = l+(r-1)/2;

if(a[mid] == x){

return mid;

if(a[mid]>x){

return search(a,l,mid-1,x);

return search(a , mid+1 , r , x);

return -1;

public class BinarySearch {

public static void main(String[] args){

Search ob = new Search();

int[] a = { 2,3,4,10,40};

int n = a.length;

int x=10;

int result = ob.search(a,0,n-1,x);

if(result == -1){

System.out.println("Element not present ");}

else{

System.out.println("Element present at index :"+result);}

Sorting : rearrangement of a given array or list of elements according to a comparison operator on the elements.
Types :
 Selection Sort
 Bubble Sort
 Insertion Sort
 Merge Sort
 Quick Sort
 Heap Sort
 Counting Sort
 Radix Sort
Selection Sort working : Selection sort works by taking the smallest element in an unsorted array and bringing it to
the front. You'll go through each item (from left to right) until you find the smallest one.
Code :
class Sorting{

void sort(int[] a){

int n = a.length;

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

int min = i;

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

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

min = j;

int temp = a[min];

a[min] = a[i];

a[i] = temp;

void print(int[] a){

int n = a.length;

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

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

System.out.println();

public class SelectionSort{

public static void main(String[] args){

Sorting ob = new Sorting();

int[] a = {64,25,12,22,11};

ob.sort(a);

System.out.println("Sorted array");

ob.print(a);

Bubble Sorting working : Bubble sort is a sorting algorithm that starts from the first element of an array and
compares it with the second element. If the first element is greater than the second, we swap them. It continues this
process until the end of the array.
Code :
public class BubbleSort {

static void sort(int[] a) {

int n = a.length;

int temp = 0;

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

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

if(a[j-1] > a[j]){

temp = a[j-1];

a[j-1] = a[j];

a[j] = temp;

public static void main(String[] args) {

int[] a ={3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");

for(int i=0; i < a.length; i++){

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

sort(a);

System.out.println("Array After Bubble Sort");

for(int i=0; i < a.length; i++){

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

Insertion Sort :

You might also like