Java Tutorial

Java Tutorial Java Features C++ vs Java Java History Java Hello World Java Development Kit Java Runtime Environment Java Virtual Machine Difference between JDK, JRE, and JVM Memory Areas in Java Java Operators Java Keywords Primitive Data Types Variables

Java Loops

Java Do While Loop Java While Loop Java For Loop Java Enhanced For Loop

Java Programs

Java Basic Programs Factorial Program in Java Fibonacci Series Program in Java Prime Number Program in Java Palindrome Number Program in Java Armstrong Number Program in Java Anagram Program in Java Pattern Programs in Java Calculator Program in Java Leap Year Program in Java Addition Program in Java Number Pattern Programs in Java Star Pattern Programs in Java Package Program in Java Pyramid Program in Java Sorting Program in Java String Palindrome Program in Java Even Odd Program in Java For Loop Program in Java If Else Program in Java Switch Case Program in Java GCD Program in Java LCM Program in Java Hello Program in Java Matrix Program in Java Menu Driven Program in Java Series Program in Java Client Server Program in Java Swapping Program in Java Pig Latin Program in Java Tower of Hanoi Program in Java Recursion Program in Java Matrix Multiplication Program in Java Perfect Number Program in Java Classes and Objects in Java Example Programs String Programs in Java Array Programs in Java Constructor Program in Java Inheritance Program in Java Abstract class Program in Java Interface Program in Java Encapsulation Program in Java Polymorphism Program in Java Exception Handling Program in Java Multithreading Program in Java Thread Program in Java Collection Programs in Java ArrayList Program in Java Stack Program in Java Applet Program in Java Swing Program in Java JDBC Program in Java How to run Java program in command prompt How to run Java program in Eclipse

Java Sorting

Sorting Algorithms in Java Merge Sort in Java Quick Sort in Java Bubble Sort in Java Insertion Sort in Java Selection Sort in Java Heap Sort in Java Radix Sort in Java Topological Sort in Java Bucket Sort in Java Counting Sort in Java

Java OOPs Concepts

OOPs - Object Oriented Programming Objects and Classes in Java Java Naming Conventions Constructors in Java Java this keyword Java static keyword Inheritance in Java Aggregation in Java Java super keyword Constructor Chaining and Constructor Overloading Java Polymorphism Static and Dynamic Binding in Java Java Abstraction Abstract class in Java Interface in Java Difference between Abstract class and Interface Java final keyword Packages in Java Access Modifiers in Java Java Wrapper classes Java Numbers Java Characters Java Integer Java Boolean Java Arrays Java Command Line Arguments Java strictfp Keyword Java Math

Java Strings

Java Strings Java String Methods StringBuilder in Java StringBuffer in Java Java Regular Expressions StringBuffer vs StringBuilder String vs StringBuffer String vs StringBuilder String Manipulation in Java Java String Concatenation How to Reverse a String in Java String Array in Java How to Compare Two Strings in Java How to Concatenate Two Strings in Java Why String in Immutable in Java java.lang.NumberFormatException for Input String String Pool in Java Java Generate Random String How to take String Input in Java Java String Interview Questions

Java Exceptions

Exception Handling in Java Java try catch Java throw Java throws Difference between throw and throws Java finally Java Custom Exception Java Exception Propagation

Garbage Collection

Automatic Resource Management in Java Java Garbage Collection Java finalize() Java gc() Difference between final, finally and finalize

Multithreading

Multithreading in Java Process and Thread in Java Basic Terms in Multithreading Java Thread creation Thread Scheduler in Java Java Thread class DeadLock in Java

Java IO

Java IO Java Read File Java BufferedWriter Java InputStreamReader Java File Java Read File Line By Line Java FileOutputStream Jar File in Java

Serialization

Java Serialization Java transient

Networking

Java Network Programming Java Socket Programming Java URL Java URLConnection HttpURLConnection in Java Java InetAddress Java DatagramSocket and Java DatagramPacket

AWT

Java AWT

Swing

Swing in Java

Java Collections

Collections in Java Java List Interface ArrayList in Java LinkedList in Java Vector in Java Stack in Java ArrayList vs LinkedList in Java ArrayList vs Vector in Java Java Set Interface HashSet in Java LinkedHashSet in Java TreeSet in Java Java Queue Interface PriorityQueue in Java ArrayDeque in Java Java Map Interface HashMap in Java LinkedHashMap in Java TreeMap in Java Dictionary in Java Hashtable in Java Properties in Java Collections class in Java Java Comparable Java Comparator Comparable vs Comparator in Java

Java Generics

Generics in Java

Java Annotations

Annotations in Java

Java JDBC

Java JDBC Tutorial

Java Differences

Java vs JavaScript Python vs Java Kotlin vs Java Java vs C++ C# vs Java Static vs Non-static in Java C vs Java int vs Integer in Java Stack vs Heap in Java Java Array vs ArrayList

How to

How to run Java program How to set path in Java How to check the Java version in cmd How to install Java in Windows 10 How to run Java program in cmd How to call a method in Java How to sort an array in Java How to iterate HashMap in Java How to write Java program How to create an array in Java How to create a package in Java How to generate random numbers in Java How to input String in Java How to create thread in Java How to find length of String in Java How to sort a string in Java How to use scanner in Java How to achieve multiple inheritance in Java How to run Java program in Eclipse How to call a function in Java How to create array of objects in Java How to create custom exception in Java How to achieve abstraction in Java How to call static method in Java

Java 8 Features

Java 8 Features Lambda Expressions in Java Functional Interface in Java Streams in Java Java Base64 Encoding and Decoding Parallel Arrays Sort in Java

Java 9 Features

Java 9 Tutorial Java 9 Try With Resources Java 9 Interface Private Method

Java Servlet Tutorial

Java Servlets Tutorial

Java JSP Tutorial

Java JSP Tutorial

Hibernate Tutorial

Hibernate Tutorial

Spring Tutorial

Spring Tutorial

Spring MVC Tutorial

Spring MVC Tutorial

Jenkins Tutorial

Jenkins Tutorial

Java Math Methods

Math.abs() Math.acos() Math.addExact() Math.asin() Math.atan () Math.atan2() Math.cbrt() Math.ceil() Math.copysign() Math.cos() Math.cosh() Math.decrementExact() Math.exp() Math.expm1() Math.floor() Math.floorDiv() Math.floorMod() Math.getExponent() Math.hypot() Math.IEEEremainder() Math.incrementExact() Math.log() Math.log10() Math.log1p() Math.max() Math.min() Math.multiplyExact() Math.multiplyFull() Math.negateExact() Math.nextAfter() Math.nextDown() Math.nextUp() Math.pow() Math.random() Math.rint() Math.round() Math.scalb() Math.signum() Math.sin() Math.sinh() Math.sqrt() Math.subtractExact() Math.tan() Math.tanh() Math.toDegrees() Math.toIntExact() Math.toRadians() Math.ulp()

Java String Methods

toCharArray() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() intern() isEmpty() join() lastIndexOf() length() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toLowerCase() toUpperCase() trim() valueOf()

Java Conversion

Java Convert String to int Java Convert int to String Java Convert String to long Java Convert long to String Java Convert String to float Java Convert float to String Java Convert String to double Java Convert double to String Java Convert String to Date Java Convert Date to String Java Convert String to Object Java Convert Object to String Java Convert String to char Java Convert char to String Java Convert int to long Java Convert long to int

Misc

Functional Interfaces in Java Singleton class in Java Awesome explanation of Strings in Java Object class in Java Static class in Java All the important string methods in Java String Handling Method in Java What are Array strings in Java Advantages and Disadvantages of Strings in Java Big Decimal class in Java Class definition in Java Char and String differences in Java Difference between String, StringBuffer and StringBuilder in java Replace character in string Java String Coding Interview Questions in Java What is String in Java? String isnullorempty in Java String Matches in Java Trim Method in String Java Bean class in Java Libraries in Java Arithmetic Operations on String in Java

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number is the highest among all. We can also sort unordered alphabets, that is, arranging alphabets from A, B, C to Z. There are different ways by which we can sort arrays in Java. These are explained below with the help of suitable programming examples.

Traditional sorting of arrays

It is possible to sort an array with the help of nested loops combined with a bunch of conditional statements. The problem with this approach is that the process is lengthy, the readability of the codes is compromised and moreover, the time complexity and the space complexity of the traditional approach is so high that it is not recommended to go with the traditional sorting method if we are working with an array of huge size.

/*Java program to sort an array using the traditional approach*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      for(int i=0;i<arr.length;i++){

         for(int j=i+1;j<arr.length;j++){

            int x=arr[i]; //Constant

            if(arr[i]>arr[j]){

               arr[i]=arr[j];

               arr[j]=x;

            }

         }    

      }

      System.out.println("Array after sorting using the traditional method:");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

49 47 62 11 33 80 87 61 79 120 147 79 20 25 135 47 120 150 25 28 14 29 131 111 13 126 72 61 11 105 85 107 119 70 90 29 137 134 144 125 45 75 57 130 64 118 6 43 114 94 30 81 9 93 146 94 127 10 139 112 135 95 78 104 45 39 3 139 12 109 71 91 68 109 65 85 121 5 114 91 28 26 42 28 95 85 77 38 143 8 86 79 101 59 48 42 106 37 61 80 133 150 39 144 10 95 50 17 90 128 101 140 138 97 150 98 140 43 95 131 42 17 18 122 126 21 84 39 40 138 47 22 111 143 83 45 46 56 94 61 30 74 34 109 13 45 82 120 4 149

Array after sorting using the traditional method:

3 4 5 6 8 9 10 10 11 11 12 13 13 14 17 17 18 20 21 22 25 25 26 28 28 28 29 29 30 30 33 34 37 38 39 39 39 40 42 42 42 43 43 45 45 45 45 46 47 47 47 48 49 50 56 57 59 61 61 61 61 62 64 65 68 70 71 72 74 75 77 78 79 79 79 80 80 81 82 83 84 85 85 85 86 87 90 90 91 91 93 94 94 94 95 95 95 95 97 98 101 101 104 105 106 107 109 109 109 111 111 112 114 114 118 119 120 120 120 121 122 125 126 126 127 128 130 131 131 133 134 135 135 137 138 138 139 139 140 140 143 143 144 144 146 147 149 150 150 150

Internal working of Array Serial Sorting

The array serial sorting uses the Merge Sort algorithm or Tim Sort algorithm to sort the contents.

This type of sorting is all done sequentially, even though merge sort uses the divide and conquer technique, it’s all done sequentially.

/*Java program to sort an array using the Arrays.sort() method*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      Arrays.sort(arr);

      System.out.println("Array after sort:");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

28 13 58 117 142 144 88 139 123 131 135 134 89 81 127 55 96 56 119 95 117 122 72 59 111 30 20 107 2 62 28 11 80 18 128 36 17 113 40 102 8 45 9 12 46 14 143 104 84 116 107 142 39 18 32 35 138 8 97 39 92 143 115 25 22 20 2 2 85 145 114 17 4 41 68 24 70 145 53 18 110 47 93 40 87 90 21 125 124 57 137 66 60 142 30 34 14 36 70 115 37 111 23 57 7 109 125 123 143 98 90 133 21 142 132 137 132 93 98 110 139 17 115 23 92 17 100 123 114 56 14 83 17 111 29 80 113 134 98 100 6 120 120 138 17 1 30 80 90 77

Array after sort:

1 2 2 2 4 6 7 8 8 9 11 12 13 14 14 14 17 17 17 17 17 17 18 18 18 20 20 21 21 22 23 23 24 25 28 28 29 30 30 30 32 34 35 36 36 37 39 39 40 40 41 45 46 47 53 55 56 56 57 57 58 59 60 62 66 68 70 70 72 77 80 80 80 81 83 84 85 87 88 89 90 90 90 92 92 93 93 95 96 97 98 98 98 100 100 102 104 107 107 109 110 110 111 111 111 113 113 114 114 115 115 115 116 117 117 119 120 120 122 123 123 123 124 125 125 127 128 131 132 132 133 134 134 135 137 137 138 138 139 139 142 142 142 142 143 143 143 144 145 145

How Array Parallel Sorting is different from Array Serial Sorting?

From a viewer perspective, the Array parallel sort and Array serial sort works almost the same and interchangeable too. Internally the array’s parallel sorting algorithm is a parallel sort-merge that is responsible for breaking the array into sub-arrays that are themselves sorted and then merged. When the length of the sub-array reaches the minimum granularity, with the help of Arrays.sort() method the sub-array is sorted. If the length of the specified array is less than the minimum granularity, then it is sorted using the appropriate Arrays.sort() method. This algorithm requires a working space smaller than or equal to the size of the specified range of the original array. The ForkJoin common pool is used to execute any parallel tasks.

Parallel Arrays Sort In Java

/*Java program to sort an array using the Arrays.parallelSort() method*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      Arrays.parallelSort(arr);

      System.out.println("Array after parallel sort:");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

106 11 110 133 103 70 79 137 94 3 33 77 112 47 31 69 66 31 107 88 36 68 146 92 140 95 44 147 87 77 149 109 49 34 25 71 23 35 92 122 59 67 139 52 118 62 94 139 141 140 80 102 83 37 133 106 104 40 92 92 7 28 30 28 128 53 123 143 71 141 28 94 90 138 44 126 25 33 68 1 83 16 88 61 51 62 46 58 132 30 64 95 32 42 126 60 74 142 40 126 123 39 29 58 97 10 87 75 22 23 26 15 122 121 24 43 61 19 99 147 96 109 7 97 83 47 67 39 52 4 112 26 62 82 63 99 146 15 140 100 145 145 38 31 31 24 109 99 107 66

Array after parallel sort:

1 3 4 7 7 10 11 15 15 16 19 22 23 23 24 24 25 25 26 26 28 28 28 29 30 30 31 31 31 31 32 33 33 34 35 36 37 38 39 39 40 40 42 43 44 44 46 47 47 49 51 52 52 53 58 58 59 60 61 61 62 62 62 63 64 66 66 67 67 68 68 69 70 71 71 74 75 77 77 79 80 82 83 83 83 87 87 88 88 90 92 92 92 92 94 94 94 95 95 96 97 97 99 99 99 100 102 103 104 106 106 107 107 109 109 109 110 112 112 118 121 122 122 123 123 126 126 126 128 132 133 133 137 138 139 139 140 140 140 141 141 142 143 145 145 146 146 147 147 149

With the help of the array parallel sort method, it is also possible to customise and sort the array within an interval. This is shown with the programming examples given below:

/*Java program to sort first 50 elements of an array using the Arrays.parallelSort() method*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      Arrays.parallelSort(arr,0,50);

      System.out.println("Array after sorting the 1st 50 elements:");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

133 120 36 118 138 141 106 68 150 70 90 90 1 4 63 17 34 7 66 112 11 10 51 100 11 150 26 97 126 91 61 32 101 88 98 67 67 128 123 56 128 113 116 67 103 67 41 122 140 103 144 91 59 107 47 136 50 57 14 88 38 117 78 103 12 62 89 87 32 145 38 111 121 98 108 97 28 39 51 117 134 114 60 95 136 131 137 17 114 84 12 102 139 11 35 17 69 119 64 130 37 140 43 68 33 105 134 102 12 80 51 119 58 69 57 36 94 133 139 8 5 83 99 62 141 91 123 80 36 64 32 54 41 142 8 80 90 14 92 23 71 145 139 28 105 85 92 54 37 111

Array after sorting the 1st 50 elements:

1 4 7 10 11 11 17 26 32 34 36 41 51 56 61 63 66 67 67 67 67 68 70 88 90 90 91 97 98 100 101 103 103 106 112 113 116 118 120 122 123 126 128 128 133 138 140 141 150 150 144 91 59 107 47 136 50 57 14 88 38 117 78 103 12 62 89 87 32 145 38 111 121 98 108 97 28 39 51 117 134 114 60 95 136 131 137 17 114 84 12 102 139 11 35 17 69 119 64 130 37 140 43 68 33 105 134 102 12 80 51 119 58 69 57 36 94 133 139 8 5 83 99 62 141 91 123 80 36 64 32 54 41 142 8 80 90 14 92 23 71 145 139 28 105 85 92 54 37 111

/*Java program to sort last 50 elements of an array using the Arrays.parallelSort() method*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      Arrays.parallelSort(arr,arr.length-50,arr.length);

      System.out.println("Array after sorting the last 50 elements:");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

28 130 85 80 40 124 76 49 64 11 2 15 100 64 67 26 129 107 19 143 134 114 64 27 103 107 106 62 36 21 10 71 54 68 150 29 53 55 90 23 150 48 82 5 57 27 62 69 97 67 118 40 116 4 94 61 30 56 143 139 143 114 27 96 41 46 131 63 137 38 56 86 46 18 111 98 79 16 133 1 97 59 1 35 22 131 107 106 76 3 140 30 98 88 73 121 135 44 71 40 30 53 22 137 8 65 74 96 24 59 5 34 134 136 76 126 21 126 100 35 77 24 73 19 145 5 143 86 128 73 55 61 17 44 77 39 134 30 40 117 134 39 53 60 20 44 99 6 56 52

Array after sorting the last 50 elements:

28 130 85 80 40 124 76 49 64 11 2 15 100 64 67 26 129 107 19 143 134 114 64 27 103 107 106 62 36 21 10 71 54 68 150 29 53 55 90 23 150 48 82 5 57 27 62 69 97 67 118 40 116 4 94 61 30 56 143 139 143 114 27 96 41 46 131 63 137 38 56 86 46 18 111 98 79 16 133 1 97 59 1 35 22 131 107 106 76 3 140 30 98 88 73 121 135 44 71 40 5 5 6 8 17 19 20 21 22 24 24 30 30 34 35 39 39 40 44 44 52 53 53 55 56 59 60 61 65 73 73 74 76 77 77 86 96 99 100 117 126 126 128 134 134 134 136 137 143 145

/*Java program to sort elements in between an array using the Arrays.parallelSort() method*/

import java.util.*;

class Main{

   public static void main(String[] args){

      int arr[]=new int[150];

      System.out.println("Array before sorting:");

      for(int i=0;i<arr.length;i++){

         arr[i]=1+(int)(Math.random()*150);

         System.out.print(arr[i]+" ");     

      }

      System.out.println();

      Arrays.parallelSort(arr,50,100);

      System.out.println("Array after sorting in between.");

      for(int i: arr){

         System.out.print(i+" ");

      }

   }

}

Output-

Microsoft Windows [Version 10.0.22000.318]

(c) Microsoft Corporation. All rights reserved.

C:\Users\USER\Desktop\JTP Folder>javac Main.java //Compile

C:\Users\USER\Desktop\JTP Folder>java Main //Run

Array before sorting:

83 26 19 145 55 48 20 143 89 112 133 92 60 100 115 88 149 39 56 88 25 33 33 7 91 1 24 93 26 62 88 141 119 114 73 32 38 101 147 27 36 33 82 27 18 113 85 13 102 110 80 26 97 73 82 146 35 129 143 111 135 56 104 140 108 89 132 134 37 28 47 135 33 43 22 34 9 84 111 35 86 92 106 148 117 33 98 30 116 24 88 124 59 55 135 52 53 61 34 136 88 44 84 78 34 28 22 105 29 42 71 141 93 149 14 109 84 59 74 122 147 89 48 140 11 105 15 22 9 131 71 78 139 142 24 139 91 138 50 65 121 134 11 109 31 98 52 123 34 97

Array after sorting in between.

83 26 19 145 55 48 20 143 89 112 133 92 60 100 115 88 149 39 56 88 25 33 33 7 91 1 24 93 26 62 88 141 119 114 73 32 38 101 147 27 36 33 82 27 18 113 85 13 102 110 9 22 24 26 28 30 33 33 34 34 35 35 37 43 47 52 53 55 56 59 61 73 80 82 84 86 88 89 92 97 98 104 106 108 111 111 116 117 124 129 132 134 135 135 135 136 140 143 146 148 88 44 84 78 34 28 22 105 29 42 71 141 93 149 14 109 84 59 74 122 147 89 48 140 11 105 15 22 9 131 71 78 139 142 24 139 91 138 50 65 121 134 11 109 31 98 52 123 34 97



ADVERTISEMENT
ADVERTISEMENT