You are on page 1of 4

CSC 185

Lab - 8
Problems
• Please write the programs based on Arrays from
CSC 201 Lecture – 8, which is found on the blog.
• Take an array and display the array elements in
reverse.
Ex: Input : { 23, 6, 4 }
Output: { 4, 6, 23 }
• Write a Java program to remove all the consonants
from the given string.
Ex: ‘csc185’ can be ‘185’
Ex: ‘computer’ can be ‘cmptr’
Bubble Sort Algorithm
1) Take an integer array of ‘n’ elements.
2) Initialize a boolean variable isNotSorted to false.
3) While the array elements are not sorted,
3.1) Loop the below code (array length – 1 times)
3.1.1) if the current element in the array is greater than
the next element
3.1.2) Swap the elements
3.1.3) Set the boolean variable isNotSorted to true.
4) Display the sorted array.
package sort;

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int temp=0;
int[] sort_array = { 110 , 45, 2, 67, 1, 900, 46};
boolean isNotSorted=true;
while(isNotSorted)
{
isNotSorted = false;
for(int i =0; i < sort_array.length - 1 ; i++)
{
if(sort_array[i] > sort_array[i+1])
{
temp = sort_array[i];
sort_array[i] = sort_array[i+1];
sort_array[i+1] = temp;
isNotSorted = true;
// System.out.println(sort_array[i]);
}
}
}
System.out.println("********************");
for(int i=0; i < sort_array.length ; i++)
System.out.println(sort_array[i]);

}
}

You might also like