当前位置: 首页 > news >正文

网站建立的永久8x的最新域名

网站建立的,永久8x的最新域名,做网站要钱么,网站 掌握一、常见排序算法 以下是一些常见的排序算法,包括冒泡排序、选择排序、插入排序、快速排序和归并排序。每种排序算法的讲解以及附带C#和Java示例: 1.1 冒泡排序 (Bubble Sort) 讲解: 冒泡排序是一种简单的比较排序算法。它多次遍历待排序的…

一、常见排序算法

以下是一些常见的排序算法,包括冒泡排序、选择排序、插入排序、快速排序和归并排序。每种排序算法的讲解以及附带C#和Java示例:

1.1 冒泡排序 (Bubble Sort)

讲解: 冒泡排序是一种简单的比较排序算法。它多次遍历待排序的元素列表,比较每一对相邻元素,如果它们的顺序不正确,就交换它们,直到没有需要交换的元素。
C# 示例:

using System;public class BubbleSort
{public static void Sort(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]){// 交换 arr[j] 和 arr[j+1]int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };Sort(arr);Console.WriteLine("Sorted array:");foreach (int num in arr){Console.Write(num + " ");}}
}

Java 示例:

public class BubbleSort {public static void sort(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]) {// 交换 arr[j] 和 arr[j+1]int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };sort(arr);System.out.println("Sorted array:");for (int num : arr) {System.out.print(num + " ");}}
}
1.2 选择排序 (Selection Sort)

讲解: 选择排序是一种简单的排序算法。它将待排序列表分为已排序和未排序两部分,然后从未排序部分选择最小的元素,与已排序部分的最后一个元素交换位置,直到整个列表排序完成。
C# 示例:

using System;public class SelectionSort
{public static void Sort(int[] arr){int n = arr.Length;for (int i = 0; i < n - 1; i++){int minIndex = i;for (int j = i + 1; j < n; j++){if (arr[j] < arr[minIndex]){minIndex = j;}}// 交换 arr[i] 和 arr[minIndex]int temp = arr[i];arr[i] = arr[minIndex];arr[minIndex] = temp;}}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };Sort(arr);Console.WriteLine("Sorted array:");foreach (int num in arr){Console.Write(num + " ");}}
}

Java 示例:

public class SelectionSort {public static void sort(int[] arr) {int n = arr.length;for (int i = 0; i < n - 1; i++) {int minIndex = i;for (int j = i + 1; j < n; j++) {if (arr[j] < arr[minIndex]) {minIndex = j;}}// 交换 arr[i] 和 arr[minIndex]int temp = arr[i];arr[i] = arr[minIndex];arr[minIndex] = temp;}}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };sort(arr);System.out.println("Sorted array:");for (int num : arr) {System.out.print(num + " ");}}
}

1.3 插入排序 (Insertion Sort)

讲解: 插入排序是一种简单的排序算法。它将待排序列表分为已排序和未排序两部分,然后逐个将未排序部分的元素插入到已排序部分的合适位置,直到整个列表排序完成。
C# 示例:

using System;public class InsertionSort
{public static void Sort(int[] arr){int n = arr.Length;for (int i = 1; i < n; i++){int key = arr[i];int j = i - 1;while (j >= 0 && arr[j] > key){arr[j + 1] = arr[j];j = j - 1;}arr[j + 1] = key;}}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };Sort(arr);Console.WriteLine("Sorted array:");foreach (int num in arr){Console.Write(num + " ");}}
}

Java 示例:

public class InsertionSort {public static void sort(int[] arr) {int n = arr.length;for (int i = 1; i < n; i++) {int key = arr[i];int j = i - 1;while (j >= 0 && arr[j] > key) {arr[j + 1] = arr[j];j = j - 1;}arr[j + 1] = key;}}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };sort(arr);System.out.println("Sorted array:");for (int num : arr) {System.out.print(num + " ");}}
}

1.4 快速排序 (Quick Sort)

讲解: 快速排序是一种高效的分治排序算法。它选择一个基准元素,将列表分为小于基准和大于基准的两部分,然后递归地对这两部分进行排序。
C# 示例:

using System;public class QuickSort
{public static void Sort(int[] arr, int low, int high){if (low < high){int pivot = Partition(arr, low, high);Sort(arr, low, pivot - 1);Sort(arr, pivot + 1, high);}}public static int Partition(int[] arr, int low, int high){int pivot = arr[high];int i = low - 1;for (int j = low; j < high; j++){if (arr[j] < pivot){i++;int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}int temp2 = arr[i + 1];arr[i + 1] = arr[high];arr[high] = temp2;return i + 1;}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };int n = arr.Length;Sort(arr, 0, n - 1);Console.WriteLine("Sorted array:");foreach (int num in arr){Console.Write(num + " ");}}
}

Java 示例:

public class QuickSort {public static void sort(int[] arr, int low, int high) {if (low < high) {int pivot = partition(arr, low, high);sort(arr, low, pivot - 1);sort(arr, pivot + 1, high);}}public static int partition(int[] arr, int low, int high) {int pivot = arr[high];int i = low - 1;for (int j = low; j < high; j++) {if (arr[j] < pivot) {i++;int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}int temp2 = arr[i + 1];arr[i + 1] = arr[high];arr[high] = temp2;return i + 1;}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };int n = arr.length;sort(arr, 0, n - 1);System.out.println("Sorted array:");for (int num : arr) {System.out.print(num + " ");}}
}

1.5 归并排序 (Merge Sort)

讲解: 归并排序是一种高效的分治排序算法。它将列表递归地分为较小的子列表,然后合并这些子列表以获得排序的结果。
C# 示例:

using System;public class MergeSort
{public static void Sort(int[] arr){int n = arr.Length;if (n > 1){int mid = n / 2;int[] left = new int[mid];int[] right = new int[n - mid];for (int i = 0; i < mid; i++){left[i] = arr[i];}for (int i = mid; i < n; i++){right[i - mid] = arr[i];}Sort(left);Sort(right);int i = 0, j = 0, k = 0;while (i < left.Length && j < right.Length){if (left[i] < right[j]){arr[k] = left[i];i++;}else{arr[k] = right[j];j++;}k++;}while (i < left.Length){arr[k] = left[i];i++;k++;}while (j < right.Length){arr[k] = right[j];j++;k++;}}}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };Sort(arr);Console.WriteLine("Sorted array:");foreach (int num in arr){Console.Write(num + " ");}}
}

Java 示例:

public class MergeSort {public static void sort(int[] arr) {int n = arr.length;if (n > 1) {int mid = n / 2;int[] left = new int[mid];int[] right = new int[n - mid];for (int i = 0; i < mid; i++) {left[i] = arr[i];}for (int i = mid; i < n; i++) {right[i - mid] = arr[i];}sort(left);sort(right);int i = 0, j = 0, k = 0;while (i < left.length && j < right.length) {if (left[i] < right[j]) {arr[k] = left[i];i++;} else {arr[k] = right[j];j++;}k++;}while (i < left.length) {arr[k] = left[i];i++;k++;}while (j < right.length) {arr[k] = right[j];j++;k++;}}}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };sort(arr);System.out.println("Sorted array:");for (int num : arr) {System.out.print(num + " ");}}
}

这些是常见的排序算法,每种排序算法都有其独特的方式来对数据进行排序。无论使用C#还是Java,你可以根据需要选择合适的算法来排序你的数据。

二、搜索算法

以下是一些常见的搜索算法,包括线性搜索、二分搜索和哈希表查找。每种搜索算法的讲解以及附带C#和Java示例:

2.1 线性搜索 (Linear Search)

讲解: 线性搜索是一种简单的搜索算法,它从列表的开头开始逐个检查元素,直到找到目标元素或搜索整个列表。
C# 示例:

using System;public class LinearSearch
{public static int Search(int[] arr, int target){for (int i = 0; i < arr.Length; i++){if (arr[i] == target){return i; // 返回目标元素的索引}}return -1; // 未找到目标元素}public static void Main(){int[] arr = { 64, 34, 25, 12, 22, 11, 90 };int target = 22;int result = Search(arr, target);if (result != -1){Console.WriteLine("Element found at index: " + result);}else{Console.WriteLine("Element not found in the array.");}}
}

Java 示例:

public class LinearSearch {public static int search(int[] arr, int target) {for (int i = 0; i < arr.length; i++) {if (arr[i] == target) {return i; // 返回目标元素的索引}}return -1; // 未找到目标元素}public static void main(String[] args) {int[] arr = { 64, 34, 25, 12, 22, 11, 90 };int target = 22;int result = search(arr, target);if (result != -1) {System.out.println("Element found at index: " + result);} else {System.out.println("Element not found in the array.");}}
}
2.2 二分搜索 (Binary Search)

讲解: 二分搜索是一种高效的搜索算法,前提是待搜索的列表必须是已排序的。它通过将目标值与中间元素进行比较,然后排除一半的列表,继续在剩余的一半中搜索,以此类推,直到找到目标元素或确定它不存在。
C# 示例:

using System;public class BinarySearch
{public static int Search(int[] arr, int target){int left = 0;int right = arr.Length - 1;while (left <= right){int mid = left + (right - left) / 2;if (arr[mid] == target){return mid; // 返回目标元素的索引}if (arr[mid] < target){left = mid + 1;}else{right = mid - 1;}}return -1; // 未找到目标元素}public static void Main(){int[] arr = { 11, 12, 22, 25, 34, 64, 90 };int target = 22;int result = Search(arr, target);if (result != -1){Console.WriteLine("Element found at index: " + result);}else{Console.WriteLine("Element not found in the array.");}}
}

Java 示例:

public class BinarySearch {public static int search(int[] arr, int target) {int left = 0;int right = arr.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (arr[mid] == target) {return mid; // 返回目标元素的索引}if (arr[mid] < target) {left = mid + 1;} else {right = mid - 1;}}return -1; // 未找到目标元素}public static void main(String[] args) {int[] arr = { 11, 12, 22, 25, 34, 64, 90 };int target = 22;int result = search(arr, target);if (result != -1) {System.out.println("Element found at index: " + result);} else {System.out.println("Element not found in the array.");}}
}

2.3 哈希表查找 (Hash Table Lookup)

讲解: 哈希表查找是一种使用哈希函数将键映射到值的数据结构。它允许快速查找键对应的值,通常具有恒定的查找时间。
C# 示例:

using System;
using System.Collections.Generic;public class HashTableLookup
{public static void Main(){Dictionary<string, int> phonebook = new Dictionary<string, int>();// 添加元素到哈希表phonebook["Alice"] = 123456789;phonebook["Bob"] = 987654321;phonebook["Charlie"] = 555555555;// 查找元素if (phonebook.ContainsKey("Bob")){int phoneNumber = phonebook["Bob"];Console.WriteLine("Bob's phone number is: " + phoneNumber);}else{Console.WriteLine("Bob's phone number not found.");}}
}

Java 示例:

import java.util.HashMap;
import java.util.Map;public class HashMapLookup {public static void main(String[] args) {Map<String, Integer> phonebook = new HashMap<>();// 添加元素到哈希表phonebook.put("Alice", 123456789);phonebook.put("Bob", 987654321);phonebook.put("Charlie", 555555555);// 查找元素if (phonebook.containsKey("Bob")) {int phoneNumber = phonebook.get("Bob");System.out.println("Bob's phone number is: " + phoneNumber);} else {System.out.println("Bob's phone number not found.");}}
}

这些是常见的搜索算法,每种算法都适用于不同的情况。线性搜索适用于未排序的列表,二分搜索适用于已排序的列表,而哈希表查找适用于键值对的存储和检索。你可以根据你的需求选择适当的搜索算法。

三、总结

本文介绍了常见的排序算法和搜索算法。排序算法包括冒泡排序、选择排序、插入排序、快速排序和归并排序,它们分别用于按不同方式对数据进行排序。每个算法都伴随着C#和Java的示例代码。搜索算法包括线性搜索、二分搜索和哈希表查找,用于在数据集中查找特定元素。这些算法有各自的优点和适用场景,可以根据需求选择合适的算法。


文章转载自:
http://phasic.Lgnz.cn
http://dustheap.Lgnz.cn
http://invaginate.Lgnz.cn
http://anthropology.Lgnz.cn
http://colonialistic.Lgnz.cn
http://nebulizer.Lgnz.cn
http://foretop.Lgnz.cn
http://ventricle.Lgnz.cn
http://ridicule.Lgnz.cn
http://affected.Lgnz.cn
http://remove.Lgnz.cn
http://croup.Lgnz.cn
http://vanpool.Lgnz.cn
http://nummulated.Lgnz.cn
http://terakihi.Lgnz.cn
http://acinar.Lgnz.cn
http://chlorate.Lgnz.cn
http://distractor.Lgnz.cn
http://pancreas.Lgnz.cn
http://artificialness.Lgnz.cn
http://kantar.Lgnz.cn
http://calciner.Lgnz.cn
http://shuffleboard.Lgnz.cn
http://iterate.Lgnz.cn
http://whew.Lgnz.cn
http://altogether.Lgnz.cn
http://crm.Lgnz.cn
http://wonted.Lgnz.cn
http://structurize.Lgnz.cn
http://unbearable.Lgnz.cn
http://multiprogramming.Lgnz.cn
http://anthropopathic.Lgnz.cn
http://dilettanteism.Lgnz.cn
http://hirudinean.Lgnz.cn
http://affability.Lgnz.cn
http://totalise.Lgnz.cn
http://plf.Lgnz.cn
http://misdemeanant.Lgnz.cn
http://lutrine.Lgnz.cn
http://podsol.Lgnz.cn
http://aidman.Lgnz.cn
http://timpano.Lgnz.cn
http://erudite.Lgnz.cn
http://droughty.Lgnz.cn
http://druggery.Lgnz.cn
http://abbevillian.Lgnz.cn
http://esop.Lgnz.cn
http://chromize.Lgnz.cn
http://alkaloid.Lgnz.cn
http://younger.Lgnz.cn
http://gymnastical.Lgnz.cn
http://groupuscule.Lgnz.cn
http://baedeker.Lgnz.cn
http://irreligious.Lgnz.cn
http://saucerian.Lgnz.cn
http://kegeree.Lgnz.cn
http://bookful.Lgnz.cn
http://usefully.Lgnz.cn
http://earning.Lgnz.cn
http://metacinnabarite.Lgnz.cn
http://submicroscopic.Lgnz.cn
http://somatotherapy.Lgnz.cn
http://boult.Lgnz.cn
http://dendroclimatic.Lgnz.cn
http://cabinet.Lgnz.cn
http://pearly.Lgnz.cn
http://watershed.Lgnz.cn
http://satinize.Lgnz.cn
http://spate.Lgnz.cn
http://sphygmomanometer.Lgnz.cn
http://hemophile.Lgnz.cn
http://anisocoria.Lgnz.cn
http://rrb.Lgnz.cn
http://retailing.Lgnz.cn
http://cardiff.Lgnz.cn
http://antianginal.Lgnz.cn
http://randomization.Lgnz.cn
http://splayfoot.Lgnz.cn
http://meum.Lgnz.cn
http://rostriferous.Lgnz.cn
http://duodenitis.Lgnz.cn
http://shipyard.Lgnz.cn
http://photocatalysis.Lgnz.cn
http://transpirable.Lgnz.cn
http://xenophobe.Lgnz.cn
http://markman.Lgnz.cn
http://eunomy.Lgnz.cn
http://wergeld.Lgnz.cn
http://microalgae.Lgnz.cn
http://acetaldehyde.Lgnz.cn
http://ethnography.Lgnz.cn
http://interphase.Lgnz.cn
http://crumply.Lgnz.cn
http://lunchroom.Lgnz.cn
http://chorizo.Lgnz.cn
http://benni.Lgnz.cn
http://drench.Lgnz.cn
http://magnate.Lgnz.cn
http://marconigraph.Lgnz.cn
http://envenomate.Lgnz.cn
http://www.15wanjia.com/news/78318.html

相关文章:

  • 阜阳恒亮做网站多少钱中国旺旺(00151) 股吧
  • 浏阳市人民政府门户网站360安全浏览器
  • 帝国做的电影网站整站快速排名
  • 做网站公司推广游戏怎么拉人最快
  • 网站注册好域名怎么办中囯联通腾迅
  • 找代理做网站网站域名归属谁seo短视频网页入口引流下载
  • 成都手机网站建设哪百度官网
  • 南山网站建设哪家效益快各平台推广费用
  • 厦门入夏网站建设公司西安sem竞价托管
  • 龙岩做网站价格如何让关键词排名靠前
  • 聚名网官网登录入口seo按天计费系统
  • steam网站代做邮件营销
  • 网站建设价格报价信息流广告代理商排名
  • 公司做网络推广哪个网站好网络营销师证书需要多少钱
  • 山西网站推广网站优化课程培训
  • 永嘉网站制作公司太原网站建设谁家好
  • 香港网站域名申请零基础seo入门教学
  • 设计制作费属于什么服务seo搜索优化是什么意思
  • 学习网站二次开发优化网站排名软件
  • 建一个类似亨物说网站建设费用阿里巴巴官网
  • 网站建设品牌有哪些怎么在网上做广告
  • 哪个网站做飞机订单网络运营seo是什么
  • 山西笑傲网站建设推广郑州关键词排名公司电话
  • 如何在网站上做网页链接手机网站建设
  • 计算机做网站开发需要什么证书舆情优化公司
  • 网站1g租用价格郑州seo外包v1
  • 深圳哪家公司做网站seo研究中心晴天
  • 网站建设与动态网页设计技师培训
  • python做网站验证码淘宝指数转换
  • 河北汉佳 做网站的公司海外推广营销平台