SoFunction
Updated on 2025-05-21

Summary of five common sorting methods in JAVA array

Preface:

Integration of several commonly used JAVA array sorting methods.

Method 1: ()

() Sort method is the easiest and most commonly used sorting method in Java

    int []arr1= {45,34,59,55};
	(arr1);//Just call the method to sort

Method 2: Bubble sorting

Simply put, bubble sorting is to repeatedly visit the sequence to be sorted, compare two elements at a time, and swap them if their order is wrong. The work of visiting the sequence is repeated until no exchange is needed, that is, the sequence has been sorted.

//array[] is the array to be sorted, n is the array lengthvoid BubbleSort(int array[], int n)
{
    int i, j, k;
    for(i=0; i<n-1; i++)
        for(j=0; j<n-1-i; j++)
        {
            if(array[j]>array[j+1])
            {
                k=array[j];
                array[j]=array[j+1];
                array[j+1]=k;
            }
        }
}

Method 3: Select sorting

First find the index of the location of the smallest element, and then swap the element with the element on the first bit.

int arr3[]= {23,12,48,56,45};
    for(int i=0;i<;i++) {
		int tem=i;
                // Assign the index of the location of the smallest element in the array starting from i to tem		for(int j=i;j<;j++) {
			if(arr3[j]<arr3[tem]) {
				tem=j;
			}
		}
		//The above is obtained the index of the position of the minimum value starting from i in the array as tem, and use this index to exchange the element on the i-th position with it		int temp1=arr3[i];
		arr3[i]=arr3[tem];
		arr3[tem]=temp1;
	}

Method 4: Inversion sorting

Arrange the original array in reverse order

//Swap the element on the i-th bit of the array with the element on the -i-1th bit of the arrayint []arr4={23,12,48,56,45};
	for(int i=0;i</2;i++) {
		int tp=arr4[i];
		arr4[i]=arr4[-i-1];
		arr4[-i-1]=tp;
	}

Method 5: Direct insertion sort

int []arr5={23,12,48,56,45};
	for (int i = 1; i < ; i++) {
		for (int j = i; j > 0; j--) {
			if (arr5[j - 1] > arr5[j]) {//The big one is placed behind				int tmp = arr5[j - 1];
				arr5[j - 1] = arr5[j];
				arr5[j] = tmp;
			}
		}
	}

This is the end of this article about the five common sorting methods in JAVA arrays. For more related Java array sorting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!