w13 Cpe121
w13 Cpe121
w13 Cpe121
Bubble Sort
We can create a java program to sort array elements using bubble sort. In bubble sort algorithm, array is traversed from first
element to last element. Here, current element is compared with the next element. If current element is greater than the next
element, it is swapped.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass
without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
C P E 1 2 1 | | S O R T I N G | | P a g e |2
LM13-PROGRAM01 LM13-PROGRAM01-OUTPUT
public class Sort {
static void bubbleSort(int[] arr)
{ int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j])
{//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp; }
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println();
LM13-PROGRAM02 LM13-PROGRAM02-OUTPUT
import java.util.Scanner;
LM13-PROGRAM03
import java.util.Scanner;
import java.util.Arrays;
// driver code
public class Sort { public static void main(String args[]) {
Scanner input = new Scanner(System.in); // create an array
// method to perform bubble sort int[] data = { -2, 45, 0, 11, -9 };
void bubbleSort(int array[]) { // create an object of Main class
int size = array.length; Sort Sort = new Sort();
// call the method bubbleSort using object bs
// for ascending or descending sort
// pass the array as the method argument
System.out.println("Number to sort: -2, 45, 0, 11, -9"); Sort.bubbleSort(data);
System.out.println("Sorting Order:"); System.out.println("Sorted Array in Ascending Order:");
System.out.println("1 for Ascending \n2 for Descending"); // call toString() of Arrays class
System.out.println("Choose a Sorting Order:[1,2]"); // to convert data into the string
int sortOrder = input.nextInt(); System.out.println(Arrays.toString(data));
// run loops two times }
// first loop access each element of the array }
for (int i = 0; i < size - 1; i++)
// second loop performs the comparison in each iteration
for (int j = 0; j < size - i - 1; j++)
// sort the array in ascending order
if (sortOrder == 1) {
if (array[j] > array[j + 1]) {
// swap if left element is greater than right
int temp = array[j];
array[j] = array[j + 1];
SELECTION SORTING
Selection sort finds the smallest element in the array and place it on the first place on the list, then it finds the second smallest
element in the array and place it on the second place. This process continues until all the elements are moved to their correct
ordering.
LM13-PROGRAM04 LM13-PROGRAM04-OUTPUT
public class SelectionSortExample {
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1)
{System.out.print(i+" ");}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1)
{System.out.print(i+" "); }
}
public static void selectionSort(int[] arr)
{ for (int i = 0; i < arr.length - 1; i++)
{ int index = i;
for (int j = i + 1; j < arr.length; j++)
{ if (arr[j] < arr[index]){
index = j;}//searching for lowest index }
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber; }
} } }
C P E 1 2 1 | | S O R T I N G | | P a g e |6
Sytnax:
The syntax of the Java for-each loop is:
// create an array
int[] numbers = {3, 9, 5, -5};
Character Methods
Following is the list of the important instance methods that all the subclasses of the Character class implement −
Strings
String is a sequence of characters, for e.g. “Hello” is a string of 5 characters. In java, string is an immutable object
which means it is constant and can cannot be changed once it has been created. In this module we will learn about
String class and String methods in detail along with many other Java Strings. In Java programming language, strings
are treated as objects. The Java platform provides the String class to create and manipulate strings.
Creating a String
There are two ways to create a String in Java
1. String literal
2. Using new keyword
Object-Oriented Programming Page 8 of 11
Sorting
String literal
In java, Strings can be created like this: Assigning a String literal to a String instance:
String str1 = "Welcome";
String str2 = "Welcome";
In this case compiler would create two different object in memory having the same string.
LM011-Program02 LM011-Program02-OUTPUT
public class {
public static void main(String args[]){
//creating a string by java string literal
String str = "—BSCpE--";
char arrch[]={'h','e','l','l','o'};
//converting char array arrch[] to string str2
String str2 = new String(arrch);
LM011-Program03 LM011-Program03-OUTPUT
public class StringDemo {
LM11-PROGRAM04 LM11-PROGRAM04-OUTPUT
public class CharString {
String Length
Methods used to obtain information about an object are known as accessor methods. One accessor method that
you can use with strings is the length() method, which returns the number of characters contained in the string object.
LM11-PROGRAM05 LM11-PROGRAM05-OUTPUT
public class StringDemo {
LM11-PROGRAM07 LM11-PROGRAM07-OUTPUT
class StringToCharDemo {
public static void main(String args[]) {
// Using charAt() method
String str = "Hello";
for(int i=0; i<str.length();i++){
char ch = str.charAt(i);
System.out.println("Character at "+i+" Position: "+ch); }
}
}
Object-Oriented Programming Page 10 of 11
Sorting
LM11-PROGRAM08 LM11-PROGRAM08-OUTPUT
String st = String.valueOf(ch);
String st2 = new String(ch);
System.out.println(st);
System.out.println(st2);
}
}
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation:
LM11-PROGRAM010 LM11-PROGRAM010
public class StringDemo {
LM11-PROGRAM011 LM11-PROGRAM011-OUTPUT
LM11-PROGRAM012 LM11-PROGRAM012-OUTPUT
public class StringMethod {