选择、插入、冒泡排序(从小到大)

本文详细介绍了插入排序、选择排序和冒泡排序算法的实现,并通过实例代码展示了它们在不同数组长度下的性能表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.wangzhu.main;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		work(new int[] { 1, 1, 1, 1, 1 });
		work(new int[] { 5, 4, 3, 2, 1 });
		work(new int[] { 1, 2, 3, 4, 5 });
		work(new int[] { 1, 7, 2, 3, 3, 4, 5, 5, 7, 8, 9, 10, 9, 8, 7, 6 });

	}

	public static void work(int[] arr) {
		insertSort(arr);
		selectSort(arr);
		bubbleSort(arr);
	}

	/**
	 * 插入排序(从小到大):在要排序的一组数中,假设前面(n-1)[n>=2]个数已经排好序,
	 * 现在要把第n个数插到前面的有序数中,使得这n个数也是排好序的。如此反复,知道全部排好序。
	 * 
	 * @param arr
	 */
	public static void insertSort(int[] arr) {
		for (int i = 1, len = arr.length, temp, j; i < len; i++) {
			temp = arr[i];
			for (j = i - 1; j >= 0 && temp < arr[j]; j--) {
				arr[j + 1] = arr[j];
			}
			arr[j + 1] = temp;
		}
		show(arr);
	}

	/**
	 * 选择排序(从小到大):在要排序的一组数中,选出最小的一个数与第一个位置的数交换;
	 * 然后在剩下的数当中再找最小的数与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
	 * 
	 * @param arr
	 */
	public static void selectSort(int[] arr) {
		for (int i = 0, len = arr.length, k; i < len; i++) {
			k = i;
			for (int j = i + 1; j < len; j++) {
				if (arr[j] < arr[k]) {
					k = j;
				}
			}
			swap(arr, i, k);
		}
		show(arr);
	}

	/**
	 * 冒泡排序(从小到大):在要排序的一组数中,对当前还未排好序的范围内的全部数, 自上而下对相邻的两个数依次进行比较和调整,
	 * 让较大的数往下沉,较小的往上冒。 即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
	 * 
	 * @param arr
	 */
	public static void bubbleSort(int[] arr) {
		for (int i = 0, len = arr.length - 1; i < len; i++) {
			for (int j = 0; j < len - i; j++) {
				if (arr[j] > arr[j + 1]) {
					swap(arr, j, j + 1);
				}
			}
		}
		show(arr);
	}

	private static void swap(int[] arr, int i, int k) {
		int temp = arr[i];
		arr[i] = arr[k];
		arr[k] = temp;
	}

	public static void show(int[] arr) {
		System.out.println("-------");
		for (int i : arr) {
			System.out.print(i + " ");
		}
		System.out.println("-------");
	}
}

 

转载于:https://www.cnblogs.com/xiaoxian1369/archive/2012/08/01/2618665.html

### 单链表在 Dev-C++ 中实现从小到大冒泡排序 为了使用单链表在 Dev-C++ 中实现从小到大冒泡排序,首先需要定义一个单链表结构体 `Node` 和其操作函数。以下是完整的解决方案: #### 定义节点结构 创建一个简单的单链表节点结构体 `struct Node`,其中包含数据域 `data` 和指针域 `next`。 ```cpp #include <iostream> using namespace std; // 定义单链表节点结构 struct Node { int data; Node* next; }; ``` #### 创建并初始化单链表 编写一个辅助函数用于向单链表中插入新节点。 ```cpp // 向链表尾部添加节点 void append(Node*& head, int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } ``` #### 冒泡排序算法 通过遍历整个单链表来模拟数组中的两层循环逻辑。外层控制排序轮数,内层负责相邻两个节点的数据比较交换。 ```cpp // 对单链表执行冒泡排序从小到大void bubbleSort(Node* head) { if (!head || !head->next) return; // 如果为空或者只有一个节点,则无需排序 bool swapped; do { swapped = false; Node* current = head; while (current && current->next) { if (current->data > current->next->data) { // 若当前节点值大于下一个节点值则交换 swap(current->data, current->next->data); swapped = true; } current = current->next; } } while (swapped); // 只有当发生过交换时才继续下一轮 } ``` #### 打印链表内容 最后提供一个打印链表的工具函数以便验证结果。 ```cpp // 遍历并打印链表的内容 void printList(Node* head) { Node* current = head; while (current) { cout << current->data << " "; current = current->next; } cout << endl; } ``` #### 主程序测试代码 将上述功能组合起来,在主函数中完成输入、排序以及输出的操作流程如下所示: ```cpp int main() { Node* head = nullptr; // 插入一些随机数值作为初始状态 int values[] = {34, 7, 23, 32, 5, 62}; for(auto val : values){ append(head, val); } cout << "Original list: "; printList(head); // 调用冒泡排序方法处理该列表 bubbleSort(head); cout << "Sorted list: "; printList(head); // 清理内存资源 Node* temp; while (head != nullptr) { temp = head; head = head->next; delete temp; } return 0; } ``` 以上即为基于 C++ 的单链表实现的小到大冒泡排序完整过程[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值