Solved question on Insertion sort, Assignments of Computer science

Exercises Sorting and Searching in the Array and DDA Solved question on Insertion sort

Typology: Assignments

2022/2023

Uploaded on 09/18/2023

aditi-mall-1
aditi-mall-1 ๐Ÿ‡ฎ๐Ÿ‡ณ

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solved question on Insertion sort
Question 1
A class Sorter contains an array of n integers. The subscripts of the array elements vary from 0 to n-1. Some
of the members of the class Sorter are given below:
Class name : Sorter
Data members/instance variables :
arr[ ] : integer array to store elements.
n : size of the array.
Member functions/methods :
Sorter( int nn ) : parameterized constructor to assign n = nn and create the array.
void Accept( ) : to input n integers into the array arr[ ].
void InsSort( ) : sorts the array in ascending order using Insertion sort method.
void showArray( ) : displays n integers.
Specify the class Sorter giving details of the constructor and functions void Accept( ), void InsSort( ) and
void showArray( ). Write a main( ) function to create an object and call the functions accordingly to input
the array, print the array before and after sorting.
Solution:
import java.util.*;
class Sorter
{
int arr[ ]; //array declaration
int n;
//parameterized constructor
Sorter( int nn )
{
n = nn;
arr = new int[ n ]; //creating array
}//end constructor
void Accept( ) //function to input the array
{
Scanner sc = new Scanner( System . in );
for( int i = 0; i < n ; i++ )
{
System.out.print("Input a number for array = " );
arr[ i ] = sc . nextInt( );
}//end loop
}//end function
pf3
pf4
pf5
pf8

Partial preview of the text

Download Solved question on Insertion sort and more Assignments Computer science in PDF only on Docsity!

Solved question on Insertion sort

Question 1 A class Sorter contains an array of n integers. The subscripts of the array elements vary from 0 to n- 1. Some of the members of the class Sorter are given below: Class name : Sorter Data members/instance variables : arr[ ] : integer array to store elements. n : size of the array. Member functions/methods : Sorter( int nn ) : parameterized constructor to assign n = nn and create the array. void Accept( ) : to input n integers into the array arr[ ]. void InsSort( ) : sorts the array in ascending order using Insertion sort method. void showArray( ) : displays n integers. Specify the class Sorter giving details of the constructor and functions void Accept( ) , void InsSort( ) and void showArray( ). Write a main( ) function to create an object and call the functions accordingly to input the array, print the array before and after sorting. Solution:

import java.util.*;

class Sorter

int arr[ ]; //array declaration

int n;

//parameterized constructor

Sorter( int nn )

n = nn;

arr = new int[ n ]; //creating array

}//end constructor

void Accept( ) //function to input the array

Scanner sc = new Scanner( System. in );

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

System.out.print("Input a number for array = " );

arr[ i ] = sc. nextInt( );

}//end loop

}//end function

void InsSort( ) //insertion sorting is done here

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

int temp = arr[ j ]; //store jth index element to extra variable โ€˜ temp โ€™ to make it safe

int d = j โ€“ 1; // storing j โ€“ 1 to variable โ€˜dโ€™ which will be used as index number to pick

// the element stored before jth index

// the following while( ) loop checks elements and perform insertion

while( d >= 0 && temp < arr[ d ] )

arr[ d + 1 ] = arr[ d ] ; // insert element of dth index of array arr[ ] to

// d+1 index of array arr[ ]

d = d โ€“ 1 ; //decrease value of d to get the previous index

}//while loop ends here

arr[ d + 1 ] = temp ; // inserting value stored in โ€˜ temp โ€™ to its actual index

}//end for j loop

}//end of the function

void showArray( )

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

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

System.out.println( arr[ i ] ); //printing array elements

}//end loop

}//end function

//making main method

public static void main( String args[ ] )

Scanner sc = new Scanner( System. in );

System.out.print("Enter the size of the array <= 100 : = " );

int N = sc. nextInt( );

The sorter array is as :

The elements of array:

Exercises

Sorting and Searching in the Array and DDA

Question 1 A class Search contains an array of 100 integers. Some of the members of the class are given below: Class name : Search Data members/instance variables : arr[ ] : to store 100 integers in the array. num : integer to store a number to search. Member functions/methods : Search( ) : default constructor. void readlist( ) : to input 100 integers into the array. void dispList( ) : to display integers present in the array. void LinSearch( ) : to accept an integer in num , using the Linear search technique search num from the array, display the number along with its index/subscript if found in the list otherwise display โ€œ Search number missing โ€. (a) Specify the class Search giving the details of the constructor and functions void readlist( ) , void dispList( ) and void LinSearch( ). Define a main( ) method to create an object and call the methods accordingly to enable the task. (b) State the difference between sorting and searching. Question 2 A class MySearch contains an array of โ€˜nโ€™ characters. Some of the members of the class are given below: Class name : MySearch Data members/instance variables : n : integer to store the maximum size of the array. chr[ ] : character array to store characters. Member functions/methods : MySearch( int nm ) : constructor to initialize data member n = nm and creates the array of size โ€˜ n โ€™. void AcceptList( ) : to input and store โ€˜ n โ€™ characters into the array chr[ ]. int LinSearch( char ele) : using the Linear search technique search argument ele from the array and return the index/subscript if found in the list otherwise returns - 1 if not found. void disp( ) : to display all the characters horizontally with at least one space between them and by invoking function int LinSearch(โ€ฆ) display the index number/subscript along with the character if found otherwise display โ€œ Search Unsuccessful โ€.. Specify the class MySearch giving the details of the constructor and functions void AcceptList( ) , int LinSearch( int ) and void disp( ). Define a main( ) method to create an object and call the methods accordingly to enable the task.

Question 5 A class Search contains roll numbers and total marks obtained in an examination in two different arrays. The roll numbers are sorted in ascending order along with total marks. Some of the members of the class are given below: Class name : Search Data members/instance variables : roll[ ] : to store roll numbers in the array. total[ ] : to store total marks obtained. max : integer to store the maximum size of the array. rolnum : integer to store the roll number. Member functions/methods : Search( int nn ) : constructor to initialize data member max = nn and create both the arrays. void FillInfo( ) : to input and store roll numbers in roll[ ] and total marks in total[ ] of max size (assume that the roll numbers are sorted in ascending order along with total marks). void Show( ) : displays the sorted list containing roll numbers and total marks in the format given below: Roll Number Total Marks xxxx xxxx xxxx xxxx xxxx xxxx void BinSearch( ) : accept roll number ( rolnum ) and search it using the Binary search technique from the sorted array of roll[ ] and displays the roll number and total marks if found in the sorted list otherwise if not found, then displays โ€œSearch roll number is not listedโ€. Specify the class Search giving the details of the constructor and functions void FillInfo( ) , void Show( ) and void BinSearch( ). Define a main( ) method to create an object and call the methods accordingly to enable the task. Question 6 A class Arrange contains an array of N integers. Some of the members of the class are given below: Class name : Arrange Data members/instance variables : num[ ] : integer array. N : integer to store size of the array. Member functions/methods : Arrange( int nn) : constructor to initialize the data member N = nn and create the integer array. void FillArray( ) : to input N integers into the array. int indexOfMin( int p ) : returns the index/subscript of the smallest element of the array using the argument p. void Sort( ) : sorts the array in ascending order by invoking the function int indexOfMin(int) and by using the Selection sort algorithm. void dispArray( ) : to display integers present in the array. Specify the class Arrange giving the details of the constructor and functions void FillArray( ) , int indexOfMin(int) , void Sort( ) and void dispArray( ). Define a main( ) method to create an object and call the methods accordingly to input the array, print the original array and the sorted array.

Question 7 A class Matrix is declared with the following details : Class name : Matrix Data members/instance variables : mat[ ][ ] : double dimensional integer array. m : integers to store size of the array. Member functions/methods : Matrix( int nm ) : parameterized constructor to assign m = nm and create the array of size m x m. void readMatrix( ) : to input elements into the matrix mat[ ][ ] of m rows and m columns. void Show( ) : to print the array in square matrix form. void modify( ) : to replace the elements of left diagonal and right diagonal of the matrix by one ( 1 ). Example : Input : m = 4 INPUT MATRIX : OUTPUT MATRIX : 3 4 5 6 1 4 5 1 5 7 3 4 6 1 1 4 4 8 6 2 4 1 1 2 5 9 7 3 1 9 7 1 Specify the class Matrix giving the details of the constructor and void readMatrix( ) , void Show( ) and void modify( ). Define a main( ) function to create an object and by invoke methods print the original matrix and modified matrix. Question 8 The transpose of a matrix is obtained by changing rows to columns and columns to rows. For example: Input Matrix Transpose Matrix 3 4 5 3 8 4 8 9 7 4 9 3 4 3 2 5 7 2 A class Transpose is declared with the following details: Class name : Transpose Data members/instance variables : arr[ ][ ] : double dimensional integer array. newArr[ ][ ] : double dimensional integer array. m : integers to store size of the array. Member functions/methods : Transpose( int nm ) : parameterized constructor to assign m = nm and create the both the arrays of size m x m. void readMatrix( ) : to input and store elements into the matrix arr[ ][ ]. void TransMat( ) : to obtain and store the transpose form of the matrix arr[ ][ ] and into the matrix newArr[ ][ ]. void Show( ) : to print the original matrix and transpose matrix. Specify the class Transpose giving the details of the constructor and void readMatrix( ) , void TransMat( ) and void Show( ). Define a main( ) function to create an object and invoke the methods.