Sorting in java
Hi, In this article we will learn about sorting in java we will discuss 4 types of sorting in java we will discuss all algorithm by explaining and make a program of these algorithms in java languages.
What is sorting in java?
Sorting means to arrange anything in a particular array we are use sorting in our day to day life. In your mobile phone, you may see an option of the sort like arrange files according to their size, date, etc. and If we talk about programming languages their is very much use of sorting we do use sorting to arrange an array from small element to big element or big element to small element. sorting means either you go small to big or big to small.
Example 1: 6, 1, 7, 4, 8, 9, 3.
Example 2: 1, 3, 4, 6, 7, 8, 9.
Example 3: 9, 8, 7, 6, 4, 3, 1.
The above you see is three examples Example 1 is a non-sorted integer array java. but Example 2 and 3 are an example of a sorted integer array java. the difference is only that first sorted integer array java is ascending order and second is descending order.
So hope you have understood what is sorting. Now we will talk about the benefits and implementation of sorting in java programming languages.
What is the use of sorting in java?
Sorting is very much useful in computing Applications like we need to search in the database regularly then it is very useful to store data in the database. like we discuss how binary search is more efficient then linear search and binary search can only be done if data is in sorted form.
What are the different types of sorting algorithms in java?
There are more than 10 types of sorting in java but in this article, we will only talk about 4 basic sorting algorithms. we will discuss some more sorting Algorithms in our Advance sorting algorithms in java.
- Java collections sort
- Bubble sort java
- Insertion sort java
- Selection sort java
Write a program of java collections sort?
Java collections sort is very easy to use you need to import the library of the collection in java and you can sort any ArrayList or array in just one line.
Here is the code for java collections sort or sort function in java
import java.util.*;
public class HelloWorld
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
int arr[] = {2,5,1,3,8,9,4,6,7};
Arrays.sort(arr);
// here 9 is the size of the array.
for(int i=0;i<9;i++)
{
System.out.print(arr[i]+" ");
}
}
}
The time complexity of this algorithm is very good unless you are not required to write particular sorting algorithms the I recommend using java collections sort Algorithms.
Write a program of Bubble sort java?
Bubble sort in java is the most popular technique to write sorting algorithms in java because bubble sort is very easy to learn so all teachers or professors start with these sorting algorithms. But honestly, bubble sort is very time-consuming sorting algorithms so I personally not prefer it to use that sorting algorithm. But if you ask to write bubble sort algorithms then you need to know the concept.
Here is the code of Bubble sort java
import java.util.*;
public class HelloWorld
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
int arr[] = {2,5,1,3,8,9,4,6,7};
// here 9 is the size of the array.
for(int i=0;i<9;i++)
{
for(int j=0;j<i;j++)
{
if(arr[i]<arr[j])
{
int val = arr[i];
arr[i] = arr[j];
arr[j] = val;
}
}
}
for(int i=0;i<9;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Concept of Bubble sort is based upon sorting and we need to use two loops that why it is very timed complex algorithms. So use it when you ask to use the Bubble sort algorithm.
Write a program of Selection sort java?
The selection sort java algorithm work that uses a technique to find the smallest element in the array then put in front one after one Selection sort can be visualized as the main array you have two subarrays one is sorted and another one is unsorted.
Here is the code for selection sort java
import java.io.*;
import java.lang.*;
import java.util.*;
public class HelloWorld
{
static void SelectionSort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static void main(String []args)
{
int arr[]= {2,1,4,3,7,5,6,9,8};
int n = arr.length;
SelectionSort(arr);
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Write a program of Insertion sort java?
Insertion sort is a simple sorting algorithm in java and works like as we are sorting playing cards in our hands before playing time complexity of insertion sort is also O(n^2) but it observes that it generally takes less time then Bubble sort.
Here is the code for insertion sort java
import java.io.*;
import java.lang.*;
import java.util.*;
public class HelloWorld
{
static void InsertionSort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static void main(String []args)
{
int arr[]= {2,1,4,3,7,5,6,9,8};
int n = arr.length;
InsertionSort(arr);
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Thanks for reading that article hope you like our article of sorting in java here at algomentor we write informative article for you guys so, please follow us on our social handle for more update on an informative blog in c++ and java languages.
Here is the list of the similar article that you might like
If you want to write a post on our website then please check this article on How to do a guest post.
0 Comments