根据给定的信息,我们可以推断出这段代码是用于找出一系列输入数字中的最大值和最小值,而不是实现一种具体的排序算法。然而,为了更好地理解和生成与“Java 排序算法”相关的知识点,我们将从以下几个方面进行深入探讨: ### Java 排序算法概述 排序算法是一种非常重要的计算机科学基础算法之一,在数据处理、检索以及分析等方面都有着广泛的应用。在Java编程语言中,排序算法通常用于对数组或集合中的元素进行排序。排序算法可以分为几大类:如冒泡排序、选择排序、插入排序、快速排序、归并排序等。 ### 1. 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。 #### 示例代码: ```java void bubbleSort(int arr[]) { int n = arr.length; for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) { // swap arr[j+1] and arr[i] int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } ``` ### 2. 选择排序(Selection Sort) 选择排序是一种简单直观的比较排序算法。它的基本思想是:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。 #### 示例代码: ```java void selectionSort(int arr[]) { int n = arr.length; // One by one move boundary of unsorted subarray for (int i = 0; i < n - 1; i++) { // Find the minimum element in unsorted array int min_idx = i; for (int j = i + 1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } ``` ### 3. 插入排序(Insertion Sort) 插入排序是一种简单直观的比较排序算法。它的基本思想是将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增1的有序表。 #### 示例代码: ```java void insertionSort(int arr[]) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } ``` ### 4. 快速排序(Quick Sort) 快速排序是一种分而治之的排序算法。其基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。 #### 示例代码: ```java int partition(int arr[], int low, int high) { int pivot = arr[high]; // pivot int i = (low - 1); // Index of smaller element for (int j = low; j <= high - 1; j++) { // If current element is smaller than the pivot if (arr[j] < pivot) { i++; // swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // swap arr[i+1] and arr[high] (or pivot) int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { // pi is partitioning index, arr[pi] is now // at right place int pi = partition(arr, low, high); // Separately sort elements before // partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } ``` ### 5. 归并排序(Merge Sort) 归并排序也是一种分而治之的排序算法。其基本思想是:将当前序列分为若干个子序列,每个子序列是有序的;然后再把有序子序列合并为整体有序序列。 #### 示例代码: ```java void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void mergeSort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } ``` ### 总结 以上介绍了一些常见的排序算法,并提供了相应的Java示例代码。这些算法各有优缺点,在实际应用中可以根据具体场景选择合适的排序方法。例如,对于较小的数据量,可以使用插入排序或选择排序;而对于大规模数据集,快速排序和归并排序通常是更好的选择。了解和掌握这些排序算法对于Java程序员来说是非常重要的。




没有把0拿进去比较哈,不然最小始终是0,如果你要比较最后输入的0,那么把判断最大最小的条件&&i!=0去掉就行,里面的接收变量名不是num,是i,你自己改下。
package sdf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class dds {
/**
* @param args
*/
public static void main(String[] args) {
int i = 1;// 非零的都行
int max, min;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入整数:");
try {
i = Integer.parseInt(br.readLine());// 赋值给i
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
max = i;// 付给最大值
min = i;// 最小值
do {
try {
System.out.println("请输入整数:");
i = Integer.parseInt(br.readLine());// 循环输入整数


- 粉丝: 0
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 档案计算机管理系统建设六个思考.doc
- 电气工程自动化工程控制系统的发展趋势及存在的问题.docx
- 《程序设计基础》课程作业评讲(1).doc
- IBM智能专家系统概述-一体机与集成系统.docx
- 湖南工业和信息化发展情况及展望.docx
- 单片机简易数字电压表设计方案.doc
- EPC项目管理要点.docx
- 机械手PLC自动控制.doc
- 坐井观天(第二课时)教学程序设计.doc
- 大数据时代对人人网营销策略的影响.docx
- 复杂网络技术在关联客户贷款集中度审计中的应用.docx
- 东财电子商务概论期末考试试题及标准答案.doc
- 事业单位档案信息化建设标准要求及措施.docx
- 煤炭企业管理信息系统集成项目中存在问题及其对策.docx
- 项目管理中沟通对象有哪些.docx
- 三菱FXplc机械手.doc


