You are on page 1of 9

Programs for 1D-Arrays Class

Test
Std XI
Bubble sort in descending order
• /*Write a program to input 10 • // create the array of size n • // bubblesort the array
integer elements in an array and
sort them in descending order • int[] x = new int[n]; • for(int i=0; i < (n-1); i++) • // display the sorted
using bubble sort technique.*/ • // fill up the array based • { array
on user input
• for(int j=0; j < (n-1-i); j++) •
• import java.util.*; • System.out.print("Enter System.out.print("Sorted
the elements separated by a • {
array (descending): ");
• class Bubble space: "); • // swap if current is lower
than next • for(int i=0; i < n; i++)
• { • for(int i=0; i < n; i++)
• // we need to move the • {
• public static void main(String[] args) • {
lowest to the end
• { • x[i] = sc.nextInt();
• System.out.print(x[i] + " ");
• if(x[j] < x[j+1])
• // for user inputs
• } • }
• Scanner sc = new Scanner(System.in); • {
• temp = sc.nextLine(); // • System.out.println();
• String temp = "";
for the trailing newline • int t = x[j];
• }
• x[j] = x[j+1];
• // ask array size (10 in the • }
• x[j+1] = t;
question)
• }
• System.out.print("Enter the
array size: "); • }
• int n = sc.nextInt(); • }
• temp = sc.nextLine(); // for
the trailing newline
Selection sort in descending order
• /*Q5. Write a program to input • // ask user to enter 10 weights • for(j=1; j <= wt.length- • // display the sorted
and store the weight of ten 1-i; j++) output
people. Sort and display them in • System.out.print("Enter the
weights on 1 line: "); • {
descending order using the •
selection sort technique.*/ • for(i=0; i < wt.length; i++) • if(wt[j] < System.out.print("Sorted
• import java.util.*; • { wt[minIndex]) weight: ");
• class Q5 • wt[i] = sc.nextInt(); • { • for(i=0; i<wt.length; i++)
• { • } • // the new • System.out.print(wt[i]
minimum index + " ");
• public static void • temp = sc.nextLine(); // for the
main(String[] args) • minIndex = j;
trailing newline • System.out.println();
• { • }
• }
• Scanner sc = new • // perform the selection sort • }
Scanner(System.in); // for user • }
input • for(i=0; i < (wt.length-1); i++) • // swap once per
iteration only
• int[] wt = new int[10]; // to • {
• // this is a selection
store 10 weights • // for each iteration, assume sort
• int i, j; the minimum index is 0
• int t = wt[minIndex];
• int min, minIndex; • minIndex = 0;
• wt[minIndex] =
• String temp; • wt[wt.length-1-i];
• wt[wt.length-1-i] = t;
• }
• /*Write a program to accept the • int index = • for(i=0; i < city.length; i++)
names of 10 cities in a single
dimension string array and their STD
(Subscriber Trunk Dialing) codes in •
huntForCity(searchCity);
if( index < 0 )
• { Linear search
• System.out.print("Enter STD-
another single dimension integer • { code and city-name: "); • // hunt for city name in
array. Search for a name of a city array
• System.out.println("Search • std[i] = sc.nextInt();
input by the user in the list. If found,
display “Search Successful” and print Unsuccessful. No such city in list."); • city[i] = • // return index of the
the name of the city along with its sc.nextLine().trim().toUpperCase();
STD code, or else display the • } location found
• System.out.println( std[i] + "."
message “Search Unsuccessful. No
such city in the list”.*/
• else + city[i] + "."); • // if not exists, return -1
• { • }
• import java.util.*; • public static int
• System.out.println("Search • } huntForCity(String
• class Q
Successful."); searchCity)
• { • // ask user to enter a
• cityname to search for
• static String[] city = new String[10];
System.out.println(city[index] + " • {
• static int[] std = new int[10];
has STD code: " + std[index] + "."); • public static String
askCityName() • for(int i=0; i <
• } city.length; i++)
• public static void main(String[] args) • {
• }
• { // fill up the array • if( city[i].equals(
• • Scanner sc = new
• askDetails(); }
Scanner( System.in ); searchCity ) )
• • // ask user to enter details of 10
// keep asking for city name
cities • System.out.print("Enter • return i;
• // till user enters EXIT
a city to get STD code: "); • return -1; // if not
• String searchCity = ""; • public static void askDetails()
• • String c = found
while( !searchCity.equals("EXIT") ) • {
sc.nextLine().toUpperCase()
• {
• Scanner sc = new Scanner( ; • }
• searchCity = askCityName(); System.in );
• if(searchCity.equals("EXIT")) • return c; • }
• int i;
• Q4. Write a program to
perform binary search on a list of
• do • public static int binarySearch(int[] x, int el) Binary
integers given below, to search for
an element input by the user, if it is
found display the element along


{
System.out.print("Enter an


{
int low = 0, high = x.length, mid;
search
integer to search, or 0 to exit: ");
with its position, otherwise display
the message “Search element not • searchFor = sc.nextInt(); • do
found” • {
• temp = sc.nextLine(); // for
• 5,7,9,11,15,20,30,45,89,97 the trailing newline
• mid = (low + high) / 2;
• [15 marks] • if(searchFor != 0)
• if(x[mid] == el) // search element is found
• A4. • {
• return mid; // exit with the index position
• import java.util.*; • int index = binarySearch(x,
searchFor); • if(el < x[mid]) // now explore the lower half
• class Q4
• if(index >= 0) • {
• {
• System.out.println("Value • high = mid - 1;
• public static void main(String[] " + searchFor + " found at index " +
args) index); • }
• { • else • else // upper half
• int[] x = •
{5,7,9,11,15,20,30,45,89,97}; • {
System.out.println("Search element
• int searchFor = 0; not found"); • low = mid + 1;
• String temp = ""; • } • }
• Scanner sc = new Scanner( • }while(searchFor != 0); // exit • }while(high > low);
System.in ); when 0
• }
• return -1; // if not found
• // keep asking for an element to
search for • }
• // until a 0 is entered • }
• /*Q6. Write a program to initialize
the given data in an array and find
• // create the array • // compute min, max, sum Min,
the minimum and maximum values
along with the sum of the given
elements.


int[] arr = new int[N];
// enter the elements of the


int min = arr[0], max=arr[0], sum = arr[0];
for(i=1; i < arr.length; i++)
Max,
• Numbers : 2 5 4 l 3

array
System.out.print("Enter the "
• { Sum
• Output : Minimum value : l
+ N + " elements: "); • min = (min < arr[i]) ? min : arr[i];
• Maximum value : 5 • max = (max > arr[i]) ? max : arr[i];
• int i;
• Sum of the elements : 15*/ • sum += arr[i];
• for(i=0; i < N; i++)
• import java.util.*; • }
• arr[i] = sc.nextInt();
• class Q6
• temp = sc.nextLine(); // for
• {
the trailing newline • // display the results
• public static void main(String[]
args) • System.out.println("Minimum value: " + min);
• { • • System.out.println("Maximum value: " +
• // user input
max);

• Scanner sc = new Scanner( • System.out.println("Sum of the elements: " +


System.in ); sum);
• System.out.print("Enter the • }
number of elements (N): ");
• int N = sc.nextInt();
• String temp;
• temp = sc.nextLine(); // for the
trailing newline
Bubble sort in ascending order
• Q7. Write program to bubble • // perform the bubble sort • // display the
sort the following set of values sorted array
in ascending order: • // initialize the array as • int i, j;
5,3,8,4,9,2,1,12,98,16 given in the question • for(i=0; i <
• for(i=0; i < (x.length-1); i++)
• OUTPUT : • int[] x = x.length; i++)
{5,3,8,4,9,2,1,12,98,16}; • for(j=0; j < (x.length-1-i); j++)
• 1 •
• if( x[j] > x[j+1] )
• 2 System.out.println(
• • { x[i] );
• 3
• 4 • // do the swap • }
• 5 • int temp = x[j]; • }
• 8 • x[j] = x[j+1];
• 9
• x[j+1] = temp;
• 12
• }
• 16
• 98

• import java.util.*; •
• class Q7
• {
• public static void
main(String[] args)
• {
Linear Search (substring)
• Q9. Write a program to • int N = 5; • // entry on a single line • String searchFor =
initialize an array of 5 sc.nextLine().toUpperCase();
names and initialize • int i; • // the phone number is after
another array with their the last space • if(searchFor.equals("QUIT"))
• // the 2 arrays
respective telephone as local variables • temp = sc.nextLine();
numbers. Search for a • break;
name input by the User, in • String[] names • lastSpaceIndex =
= new String[N]; temp.lastIndexOf(" "); • int searchIndex = -1;
the list. If found, display
“Search Successful ” and • long[] phones • names[i] = temp.substring(0, • for(i=0; i < names.length; i++)
print the name along with = new long[N]; lastSpaceIndex).toUpperCase();
the telephone number, • if( names[i].equals(searchFor) )
otherwise display “Search • String temp; • phones[i] = Long.parseLong(
temp.substring(lastSpaceIndex+1) ); • {
unsuccessful. Name not
• int
enlisted”.
lastSpaceIndex; • } • searchIndex = i;
• import java.util.*; • break;
• // user input • // ask the user to enter a name
• class Q9 for the 2 arrays to search
• }
• { • for(i=0; • // user to enter QUIT to exit.
i<names.length; • if( searchIndex < 0 )
• public static void i++) • while(true)
main(String[] args) • System.out.println("Search
• { • { unsuccessful. Name not enlisted.");
• {
• System.out.print("Enter a • else
• // local variables System.out.print("Ent name to search for (QUIT to exit):
er the name and "); • System.out.println("Name: " +
• Scanner sc = new
Scanner( System.in ); phone #" + (i+1) + ": • names[searchIndex] + " has Tel No: " +
"); phones[i]); }}}
Selection Sort in descending order (substring)
• Q5. Write a program that asks the user to enter
an integer (N). Create a single-dimensional
• // create the array • // perform the • // display the output
selection sort
array of N double values. Ask the user to enter • double[] d = new •
the N double values to populate this array. Sort
this array in reverse (descending) order using double[N]; • // in reverse order System.out.print("Sorted
the selection sort method, and print the same
on a single line, each element separated by a • // populating the array • int minIndex = 0; output: ");
space.
• • double swapper; • for(i=0; i<N; i++)
• import java.util.*; System.out.print("Enter
the " + N + " elements: "); • for(i=0; i < (N-1); i++) • System.out.print(d[i]
• class Q5 + " ");
• int i, j; • {
• { public static void main(String[]
args) • minIndex = 0; • System.out.println();
• for(i=0; i<N; i++)
• { // user input • { • for(j=1; j<=(N-1-i); j++) • }
• Scanner sc = new Scanner( • d[i] = • { • }
System.in ); sc.nextDouble(); • if(d[minIndex] > d[j])
• String temp; • } • minIndex = j;
• System.out.print("Enter • temp = sc.nextLine(); • }
number of elements (N): ");
• // do the swap using temp
• int N = sc.nextInt(); variable swapper
• temp = sc.nextLine(); // for the • swapper = d[minIndex];
trailing newline
• d[minIndex] = d[N-1-i];

• d[N-1-i] = swapper;

You might also like