Posts

Showing posts with the label Algorithms

Bubble Sort Algorithm | Java Programs

Output: *** Bubble Sort Algorithm *** The Original Array is: [10, 2, 7, 11, 9] After applying BUBBLE SORT algorithm . The Array becomes: [2, 7, 9, 10, 11] Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class BubbleSortAlgorithm { public static void main ( String [] args ) { int [] myArray = {10, 2, 7, 11, 9}; System . out . println ( "*** Bubble Sort Algorithm ***" + "\n" ); System . out . println ( "The Original Array is: " + Arrays . toString ( myArray )); for ( int i = 0; i < myArray . length ; i ++) { for ( int j = 0; j < myArray . length - 1; j ++) { if ( myArray [ j ] > myArray [ j + 1]) { int a = myArray [ j + 1]; myArray [ j + 1] = myArray [ j ]; myArray [ j ] = a ; } } ...

Recursive Bubble Sort Algorithm | Java Programs

Output: *** Recursive Bubble Sort *** Before Bubble Sort Algorithm: [64, 34, 25, 12, 22, 11, 90] After Bubble Sort Algorithm: [11, 12, 22, 25, 34, 64, 90] Click Here For  Java Online Compiler Solution: /** * * @author Alee Ahmed Kolachi */ import java.util.Arrays ; public class RecursiveBubbleSort { static void bubbleSort ( int myArray [], int n ) { if ( n == 1) { return ; } // First Pass // Largest Element would be bubble to last. for ( int i = 0; i < n - 1; i ++) { if ( myArray [ i ] > myArray [ i + 1]) { int temp = myArray [ i ]; myArray [ i ] = myArray [ i + 1]; myArray [ i + 1] = temp ; } } // Largest element is fixed, bubbleSort ( myArray , n - 1); } public static void main ( String [] args ) { int myArray [] ...

Merge Sort Algorithm | Java Programs

Output: *** Merge Sort *** --- Array Before --- 45, 23, 11, 89, 77, 9, --- Array After --- 9, 11, 23, 45, 77, 89, Click Here For  Java Online Compiler Solution: /** * * source code by: * http://www.java2novice.com */ public class MergeSort { private int [] array ; private int [] tempMergArr ; private int length ; public static void main ( String a []) { int [] inputArr = {45, 23, 11, 89, 77, 9}; System . out . println ( "*** Merge Sort ***" + "\n" ); System . out . println ( "--- Array Before ---" ); for ( int i : inputArr ) { System . out . print ( i + ", " ); } MergeSort mms = new MergeSort (); mms . sort ( inputArr ); System . out . println ( "\n" ); System . out . println ( "--- Array After ---" ); for ( int i : inputArr ) { ...

Quick Sort Algorithm | Java Programs

Output: *** Quick Sort Algorithm *** --- Array Before --- 52, 45, 7, 9, 86, 12, 5, 1, --- Array After --- 1, 5, 7, 9, 12, 45, 52, 86, Click Here For  Java Online Compiler Solution: /** * * Source Code Taken from : http://www.vogella.com */ public class QuickSort { private int [] numbers ; private int number ; public void sort ( int [] values ) { this . numbers = values ; number = values . length ; quicksort (0, number - 1); } private void quicksort ( int low , int high ) { int i = low , j = high ; // Get the pivot element from the middle of the list int pivot = numbers [( low + high ) / 2]; // Divide into two lists while ( i <= j ) { // If the current value from the left list is smaller then the pivot // element then get the next element from the left list while ( numbe...

Insertion Sort Algorithm | Java Programs

Output: *** Insertion Sort *** --- Array Before --- 23, 54, 67, 54, 12, 77, 48, 4, --- Array After --- 4, 12, 23, 48, 54, 54, 67, 77, Click Here For  Java Online Compiler Solution: /** * * @author Alee Ahmed Kolachi */ public class InsertionSort { public static int [] sortInsertion ( int [] array ) { int temporary ; for ( int i = 1; i < array . length ; i ++) { for ( int j = i ; j > 0; j --) { if ( array [ j ] < array [ j - 1]) { temporary = array [ j ]; array [ j ] = array [ j - 1]; array [ j - 1] = temporary ; } } } return array ; } public static void main ( String a []) { int [] array1 = {23, 54, 67, 54, 12, 77, 48, 4}; System . out . println ( "*** Insertion Sort ***" + "\n...

Selection Sort Algorithm | Java Programs

Output: *** Selection Sort Algorithm *** --- Array Before --- 98, 32, 21, 26, 77, 7, 8, 421, --- Array After --- 7, 8, 21, 26, 32, 77, 98, 421, Click Here For  Java Online Compiler Solution: /** * * @author Alee Ahmed Kolachi */ public class SelectionSort { public static int [] sortMethod ( int [] array ) { for ( int i = 0; i < array . length - 1; i ++) { int index = i ; for ( int j = i + 1; j < array . length ; j ++) { if ( array [ j ] < array [ index ]) { index = j ; } } int smaller = array [ index ]; array [ index ] = array [ i ]; array [ i ] = smaller ; } return array ; } public static void main ( String a []) { int [] array1 = {98, 32, 21, 26, 77, 7, 8, 421}; System . out . println ( ...

Source Code for Binary Search Algorithm

Output: *** Binary Search Algorithm *** Enter number of elements: 5 Enter 5 numbers in ascending order: 1 5 9 14 17 Enter value to find: 9 9 found at location 3. Click Here For  Java Online Compiler Solution: import java.util.Scanner ; class BinarySearch { public static void main ( String args []) { int c , first , last , middle , n , search , array []; Scanner in = new Scanner ( System . in ); System . out . println ( "*** Binary Search Algorithm ***" + "\n" ); System . out . print ( "Enter number of elements: " ); n = in . nextInt (); array = new int [ n ]; System . out . print ( "Enter " + n + " numbers in ascending order: " ); for ( c = 0; c < n ; c ++) { array [ c ] = in . nextInt (); } System . out . print ( "\nEnter value to find: " ); search =...

Recursive Insertion Sort | Java Programs

Output: *** Recursive Insertion Sort *** Array Before Insertion Sort: [12, 11, 13, 5, 6] Array After Insertion Sort: [5, 6, 11, 12, 13] Click Here For  Java Online Compiler Solution: /** * * @author Alee Ahmed Kolachi */ import java.util.Arrays ; public class RecursiveInsertionSort { static void recursiveISort ( int array [], int n ) { if ( n <= 1) { return ; } recursiveISort ( array , n - 1); // Insert last element at its correct position int last = array [ n - 1]; int j = n - 2; while ( j >= 0 && array [ j ] > last ) { array [ j + 1] = array [ j ]; j --; } array [ j + 1] = last ; } public static void main ( String [] args ) { System . out . println ( "*** Recursive Insertion Sort ***" + "\n" ); System . out . println ( ...