Eight Common Sorting Algorithms in Java with Full Code Implementations
This article explains eight classic sorting algorithms—direct insertion sort, Shell sort, simple selection sort, heap sort, bubble sort, quick sort, merge sort, and radix sort—detailing their step‑by‑step logic, when to use each, and providing complete Java code examples for every method.
1. Direct Insertion Sort
Frequently encountered when a new element must be inserted into an already sorted sequence. The algorithm builds a sorted subsequence by repeatedly inserting the next element into its correct position.
public void insertSort(int[] a){
int length=a.length; // array length for speed
int insertNum; // element to insert
for(int i=1;i
=0 && a[j]>insertNum){ // shift larger elements right
a[j+1]=a[j];
j--;
}
a[j+1]=insertNum; // place the element
}
}2. Shell Sort
Improves insertion sort for large data sets by sorting elements that are a certain gap apart, gradually reducing the gap until it becomes 1, at which point a simple insertion sort finishes the job.
public void sheelSort(int[] a){
int d=a.length;
while(d!=0){
d=d/2;
for(int x=0;x
=0 && temp3. Simple Selection Sort
Useful for extracting the smallest (or largest) elements from a sequence. It repeatedly selects the minimum element from the unsorted portion and swaps it with the first unsorted position.
public void selectSort(int[] a){
int length=a.length;
for(int i=0;i4. Heap Sort
An optimized version of selection sort that first builds a max‑heap from the array, then repeatedly swaps the heap root with the last element and restores the heap property.
public void heapSort(int[] a){
System.out.println("开始排序");
int arrayLength=a.length;
for(int i=0;i
=0;i--){
int k=i;
while(k*2+1<=lastIndex){
int biggerIndex=2*k+1;
if(biggerIndex5. Bubble Sort
Generally not recommended for large data sets. It repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order, bubbling the largest element to the end each pass.
public void bubbleSort(int[] a){
int length=a.length;
int temp;
for(int i=0;i
a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}6. Quick Sort
Chosen when the fastest sorting time is required. It selects a pivot, partitions the array into elements less than and greater than the pivot, and recursively sorts the partitions.
public static void quickSort(int[] numbers,int start,int end){
if(start
base && j>start) j--;
if(i<=j){
int temp=numbers[i];
numbers[i]=numbers[j];
numbers[j]=temp;
i++; j--;
}
}while(i<=j);
if(start
i) quickSort(numbers,i,end);
}
}7. Merge Sort
Second only to quick sort in speed, suitable when memory is limited or when parallel processing is possible. It repeatedly merges adjacent sorted sub‑arrays into larger sorted sequences.
public static void mergeSort(int[] numbers,int left,int right){
int t=1;
int size=right-left+1;
while(t8. Radix Sort
Best for sorting large numbers or very long integers. It processes the numbers digit by digit, from least significant to most significant, using bucket queues for each base‑10 digit.
public void sort(int[] array){
int max=array[0];
for(int i=1;i
max) max=array[i];
int time=0;
while(max>0){ max/=10; time++; }
java.util.List
> queue=new java.util.ArrayList<>();
for(int i=0;i<10;i++) queue.add(new java.util.ArrayList<>());
for(int i=0;i
bucket=queue.get(x);
bucket.add(array[j]);
queue.set(x,bucket);
}
int count=0;
for(int k=0;k<10;k++){
while(queue.get(k).size()>0){
java.util.ArrayList
bucket=queue.get(k);
array[count++]=bucket.remove(0);
}
}
}
}Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.