In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java bubble sort code how to achieve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that you read this Java bubble sort code how to achieve the article will have a harvest, let's take a look at it.
Bubbling sort
Bubble sorting (Bubble Sort) is a simple sorting algorithm. It repeatedly iterates through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of traversing a sequence is repeated until no more swaps are needed, that is, the sequence has been sorted. The algorithm gets its name because smaller elements float slowly to the top of the sequence by swapping.
Bubble sorting process:
By comparing the adjacent elements, we can judge whether the positions of the two elements need to be exchanged.
Compare nMY once, and the element at the end will be the largest element.
Repeat the above bubbling process n times
Code implementation:
Import java.util.Arrays;public class bubble {public static void main (String [] args) {/ / create data int [] array = {426 Magazine 375474 8465453}; / / implement sorting System.out.println (Arrays.toString (array)); System.out.println (Arrays.toString (bubbleSort (array) } public static int [] bubbleSort (int [] array) {/ / if the array is empty, return if (array.length = = 0) {return array;} / / perform the bubbling process n times, n is the array length for (int I = 0; I)
< array.length; i++) { // 冒泡过程 for (int j = 0; j < array.length - 1 - i; j++) { // 判断j索引的元素是否比j+1索引的元素大 if (array[j+1] < array[j]) { // 交换位置 int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } return array; }} 输出结果: [426, 375474, 8465, 453] [426, 453, 8465, 375474] 插入排序 插入排序 (Insertion Sort) 是一种简单直观的排序算法. 它的工作原理是通过构建有序序列, 对于未排序数据, 在已排序序列中从后向前扫描,找到相应位置并插入. 插入排序在实现上, 在从后向前扫描过程中, 需要反复把已排序元素逐步向后挪位, 为最新元素提供插入空间. 插入排序流程: 从第二个元素开始, 从后往前扫描 逐个比较元素大小, 之道插入到合适的位置 重复以上步骤 n-1 次 代码实现: import java.util.Arrays;public class insert { public static void main(String[] args) { // 创建数据 int[] array = {426,375474,8465,453}; // 实现排序 System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(insertionSort(array))); } public static int[] insertionSort(int[] array) { // 如果数组为空, 返回 if (array.length == 0) return array; // 待排序元素 int current; // 执行插入过程n-1次 for (int i = 0; i < array.length - 1; i++) { // 指定待排序元素 current = array[i + 1]; int preIndex = i; // 执行插入过程, 往前一个个比对 while (preIndex >= 0 & & current
< array[preIndex]) { array[preIndex + 1] = array[preIndex]; preIndex--; } // 插入元素 array[preIndex + 1] = current; } return array; }} 输出结果: [426, 375474, 8465, 453] [426, 453, 8465, 375474] 归并排序 归并排序 (Merge Sort) 是一种建立在归并操作上的算法, 是分治的一个经典应用. 归并排序流程: 把数组拆分成两个 n/2 长度的子序列 对这两个子序列分别采用归并排序 将排序好的序列合并成最终序列 代码实现: import java.util.Arrays;public class merge { public static void main(String[] args) { // 创建数据 int[] array = {426,375474,8465,453}; // 实现排序 System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(mergeSort(array))); } public static int[] mergeSort(int[] array) { // 如果数组长度小于2, 返回 if (array.length < 2) { return array; } // 分治 int mid = array.length / 2; int[] left = Arrays.copyOfRange(array, 0, mid); int[] right = Arrays.copyOfRange(array, mid, array.length); return merge(mergeSort(left), mergeSort(right)); } public static int[] merge(int[] left, int[] right) { // 创建数组用于存放合并后的序列 int[] result = new int[left.length + right.length]; for (int index = 0, i = 0, j = 0; index < result.length; index++) { // 左序列取完 if (i >= left.length) result [index] = right [jacks +]; / / the right sequence fetches else if (j > = right.length) result [index] = left [iTunes +]; / / the I of the left sequence is larger than the j th else if of the sequence (left [I] > right [j]) result [index] = right [jacks +] Else result [index] = left [iTunes +];} return result;}}
Output result:
[426, 375474, 8465, 453]
[426, 453, 8465, 375474]
Quick sort
Quick sorting (Quicksort) divides the sorted data into two independent parts through a single sort, in which all the data in one part is smaller than that in the other part, and then quickly sorts the two parts of data respectively according to this method, and the whole sorting process can be carried out recursively, so that the whole data becomes an ordered sequence.
Quick sort process:
Pick an element from the array as the Pivot, usually the first or last element will compare to the base element
Small values are placed in front of the datum, and values larger than the datum elements are placed behind the datum.
Recursively perform partition operations
Code implementation:
Import java.util.Arrays;public class quick {public static void main (String [] args) {/ / create data int [] array = {426,375474, 8465, 453}; / / implement sorting System.out.println (Arrays.toString (array)); quickSort (array, 0, array.length-1); System.out.println (Arrays.toString (array)) } public static void quickSort (int [] arr, int low, int high) {/ / define int p, I, j, temp; if (low > = high) {return;} / / p is the datum, the first p of each array is arr [low]; I = low; j = high; while (I
< j) { //右边当发现小于p的值时停止循环 while (arr[j] >= p & & I < j) {jmurf;} / / this must start on the right, and the upper and lower loops cannot be exchanged (parsing below, you can think about it first) / / stop the loop while on the left when a value greater than p is found (arr [I])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.