[go: up one dir, main page]

0% found this document useful (0 votes)
52 views12 pages

DSA Assignment 4

The document contains 4 programming questions and answers related to sorting and searching algorithms. Q1 asks to write a program to sort an integer array using bubble sort and display the sorted numbers. Q2 asks to write a method to search an integer array for a key element and return true if found. Q3 asks to write methods to search integer and string arrays for a key element/string and return a boolean. Q4 asks to write a method to sort an integer array using insertion sort and display the sorted elements.

Uploaded by

PS
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)
52 views12 pages

DSA Assignment 4

The document contains 4 programming questions and answers related to sorting and searching algorithms. Q1 asks to write a program to sort an integer array using bubble sort and display the sorted numbers. Q2 asks to write a method to search an integer array for a key element and return true if found. Q3 asks to write methods to search integer and string arrays for a key element/string and return a boolean. Q4 asks to write a method to sort an integer array using insertion sort and display the sorted elements.

Uploaded by

PS
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/ 12

Assignment 4

Q1: Write an application to store ‘N’ numbers of type integers and sort it using below explained logic (Bubble sort).
Your program should display the sorted numbers in a formatted way
Answer:
Program:
package package2;
import java.util.*;
public class DataStructure {
public static void main(String[]args)
{
Programming pt = new Programming();
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of array :");
int n= input.nextInt();
int[] progarray = new int[n];

for(int i=0;i<progarray.length;i++)
{
progarray[i] = input.nextInt();
}
System.out.println("The array elements are :");

for(int i=0;i<progarray.length;i++)
{
System.out.println(progarray[i]+" ");
}
pt.sortprog(progarray);
pt.display(progarray);
}
}
class Programming
{

public void sortprog(int[] progarray)


{
int len = progarray.length;
int temp = 0;
for(int i=0; i < len; i++)
{

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


{
if(progarray[j-1] > progarray[j])
{
temp = progarray[j-1];
progarray[j-1] = progarray[j];
progarray[j] = temp;
}

}
}
}

public void display(int[] progarray)


{
int len = progarray.length;
System.out.println("The aray after doing bubble sort :");
for(int i=0; i <len ; i++)
{
System.out.print(progarray[i]+" ");
}

}
}
Output:
Enter the size of array :
5
Enter the key element:
4
51428
The array elements are :
5
1
4
2
8
The aray after doing bubble sort :
12458

Q2. Write a method which accepts an integer array and key element to search. It should return
‘true’ if given key element found otherwise ‘false’
Answer :
Program:
package package2;
import java.util.*;
public class DataStructure {
public static void main(String[]args)
{
Programming pt = new Programming();
Scanner input = new Scanner(System.in);
System.out.println("Enter the size of array :");
int n= input.nextInt();
System.out.println("Enter the key element: ");
int k = input.nextInt();
int[] progarray = new int[n];

for(int i=0;i<progarray.length;i++)
{
progarray[i] = input.nextInt();
}
System.out.println("The array elements are :");

for(int i=0;i<progarray.length;i++)
{
System.out.println(progarray[i]+" ");
}
pt.sortprog(progarray);
pt.display(progarray);
boolean result = pt.findElement(progarray,k);
System.out.println(result);
}
}
class Programming
{
int flag = 0;
public void sortprog(int[] progarray)
{
int len = progarray.length;
int temp = 0;
for(int i=0; i < len; i++)
{

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


{
if(progarray[j-1] > progarray[j])
{
temp = progarray[j-1];
progarray[j-1] = progarray[j];
progarray[j] = temp;
}

}
}
}
public void display(int[] progarray)
{
int len = progarray.length;
System.out.println("The aray after doing bubble sort :");
for(int i=0; i <len ; i++)
{
System.out.print(progarray[i]+" ");
}
System.out.println();
}
public Boolean findElement(int progarray[], int k)
{
int len = progarray.length;
boolean a=false ;

for(int i=0; i < len; i++)


{
if(progarray[i]==k)
{
flag++;
break;
}
}
if(flag>0)
{
a = true;
}
return a;
}
}
Output:
Enter the key element:
4
51428
The array elements are :
5
1
4
2
8
The aray after doing bubble sort :
12458
True

Q3 Write two methods:

a) Boolean findElement(int arr[], int key): Should return ‘true’ if key element found otherwise

‘false’

b) Boolean findString(String names[], String name): Should return ‘tru’ if name found in the list otherwise ‘false’.

Answer:

Program:

package package2;

import java.util.*;

public class DataStructure {

public static void main(String[]args)

Programming pt = new Programming();

Scanner input = new Scanner(System.in);

System.out.println("Enter the size of array :");

int n= input.nextInt();

System.out.println("Enter the key element: ");

int k = input.nextInt();

int[] progarray = new int[n];

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

{
progarray[i] = input.nextInt();

System.out.println("The array elements are :");

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

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

pt.sortprog(progarray);

pt.display(progarray);

boolean result = pt.findElement(progarray,k);

System.out.println(result);

String[] a = { "SUP", "RAT", "IKD", "EYS" };

String key = "SUP";

boolean result1 = pt.findstring(a,key);

System.out.println(result1);

class Programming

int flag = 0;

public void sortprog(int[] progarray)

int len = progarray.length;

int temp = 0;

for(int i=0; i < len; i++)

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


{

if(progarray[j-1] > progarray[j])

temp = progarray[j-1];

progarray[j-1] = progarray[j];

progarray[j] = temp;

public boolean findstring(String[] a, String key)

int min = 0;

int max = a.length - 1;

int mid;

int flag=0;

boolean art=false;

while (min <= max)

mid = (min + max) / 2;

int res = a[mid].compareTo(key);


if (res == 0)

flag = flag +1;

//System.out.println(flag);

break;

else if(res>0)

min = mid +1;

else

max = mid-1;

if(flag>0)

art = true;

return art;

}
public void display(int[] progarray)

int len = progarray.length;

System.out.println("The aray after doing bubble sort :");

for(int i=0; i <len ; i++)

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

System.out.println();

public Boolean findElement(int progarray[], int k)

int len = progarray.length;

int low = 0;

int high = len-1;

int mid = (low+high)/2;

boolean a=false ;

while(low<high)

if(progarray[mid]==k)

flag++;

break;

else if(k>progarray[mid])

low = mid+1;

}
else if(k<progarray[mid])

high = mid-1;

mid = (low+high)/2;

if(flag>0)

a = true;

return a;

Output:

Enter the size of array :

Enter the key element:

51428

The array elements are :

The aray after doing bubble sort :

12458

true

true
Q4; Write a method which accepts array of unsorted integer elements and display elements in sorted order. Use
insertion sort algorithm to sort.
Program:

public class InsertionSort{  
    public static void insertionSort(int array[]) {  
        int n = array.length;  
        for (int j = 1; j < n; j++) {  
            int key = array[j];  
            int i = j-1;  
            while ( (i > -1) && ( array [i] > key ) ) {  
                array [i+1] = array [i];  
                i--;  
            }  
            array[i+1] = key;  
        }  
    }  
       
    public static void main(String a[]){    
        int[] arr1 = {9,14,3,2,43,11,58,22};    
        System.out.println("Before Insertion Sort");    
        for(int i:arr1){    
            System.out.print(i+" ");    
        }    
        System.out.println();    
            
        insertionSort(arr1);//sorting array using insertion sort    
           
        System.out.println("After Insertion Sort");    
        for(int i:arr1){    
            System.out.print(i+" ");    
        }    
    }    
}    

You might also like