基于for循环的几个常用排序方法

本文介绍了两种基本的排序算法——选择排序和冒泡排序,并提供了详细的C语言实现代码。通过对数组元素的比较和交换,这两种算法能有效地对数据进行排序。

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



  /*
 选择排序。
 */

#include <stdio.h>

int main()
{

  int arr[]={34,12,55,53,23,3,43}; // 当然自己也可以手动输入

  int len = sizeof(arr)/sizeof(int);  //求数组长度
  int x,y,z;

  for(x=0; x<len-1; x++)
  {
   for(y=x+1; y<len; y++)
   {
    if(arr[x]>arr[y])
    {
     int temp  = arr[x];   //交换两个元素顺序
     arr[x] = arr[y];
     arr[y] = temp;
     }
    }
  }
  for(z=0;z<len;z++)
  {
                    printf("arr[%d] = %d\n",z,arr[z]);
                    }
  while(1);

  return 0;
}


/*
 冒泡排序: 通过两两直接比较,把最大的那个数值放在最高位,依次进行
 */
#include <stdio.h>

int main()
{
    int arr[]={34,12,55,53,23,3,43}; // 当然自己也可以手动输入
   
    int len = sizeof(arr)/sizeof(int);  //求数组长度
    int x,y,z,temp = 0;

 for(x=0;x<len-1; x++)              //for外循环  控制数组角标元素   
 {
  for(y=0;y<len-1-x; y++)       /*
                                        for内循环  控制每次循环 两两元素比较的次数
                                        len-1-x表达式解析:
                                        -1:为了避免角标越界
                                        -x:为了让外循环增加一次,内循环参与比较的元素个数递减
                                      */
  {
   if(arr[y]>arr[y+1]) 
   {
   
    temp  = arr[y];     //比较之后前后交换元素
    arr[y] = arr[y+1];
    arr[y+1] = temp;
   
   }
  } 
 }
 
    for(z=0;z<len;z++)                 //遍历输出排序后的数组
    {
                      printf("arr[%d] = %d\n",z,arr[z]);
                      }
    while(1);
    return 0;
}


### 回答1: 快速排序是一种常用排序算法,使用 Java 实现快速排序有以下几种方法: 1. 递归方法 ``` public class QuickSort { public static void sort(int[] arr, int low, int high) { if (arr == null || arr.length == 0) return; if (low >= high) return; int middle = low + (high - low) / 2; int pivot = arr[middle]; int i = low, j = high; while (i <= j) { while (arr[i] < pivot) { i++; } while (arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } if (low < j) sort(arr, low, j); if (high > i) sort(arr, i, high); } public static void main(String[] args) { int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1}; sort(arr, 0, arr.length-1); for (int i : arr) { System.out.print(i + " "); } } } ``` 2. 非递归方法 ``` import java.util.Stack; public class QuickSort2 { public static void sort(int[] arr) { if (arr == null || arr.length == 0) return; Stack<Integer> stack = new Stack<Integer>(); stack.push(0); stack.push(arr.length - 1); while (!stack.isEmpty()) { int high = stack.pop(); int low = stack.pop(); int pivot = partition(arr, low, high); if (pivot - 1 > low) { stack.push(low); stack.push(pivot - 1); } if (pivot + 1 < high) { stack.push(pivot + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; int i = low + 1; int j = high; while (i <= j) { while (i <= j && arr[i] < pivot) { i++; } while (i <= j && arr[j] > pivot) { j--; } if (i <= j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] ### 回答2: 快速排序是一种常用排序算法,它的基本思想是通过划分将数据分成较小和较大的两部分,递归地对两部分进行排序,最终实现整个序列的有序。 在Java中实现快速排序的几种常用方法如下: 1. 递归实现: ```java public static void quickSort(int[] arr, int low, int high) { if (low < high) { int partitionIndex = partition(arr, low, high); quickSort(arr, low, partitionIndex - 1); quickSort(arr, partitionIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } ``` 2. 非递归实现: ```java public static void quickSort(int[] arr) { if (arr == null || arr.length <= 1) { return; } Stack<Integer> stack = new Stack<>(); stack.push(0); stack.push(arr.length - 1); while (!stack.isEmpty()) { int high = stack.pop(); int low = stack.pop(); int partitionIndex = partition(arr, low, high); if (low < partitionIndex - 1) { stack.push(low); stack.push(partitionIndex - 1); } if (high > partitionIndex + 1) { stack.push(partitionIndex + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } ``` 以上就是使用Java实现快速排序的几种常用方法和代码。快速排序的时间复杂度为O(nlogn),是一种高效的排序算法。 ### 回答3: 快速排序是一种经典的排序算法,可以使用多种方式实现。下面介绍两种常见的 Java 实现方法。 一种常见的快速排序实现方法是使用递归。首先选择一个基准元素(通常是数组的第一个元素),将数组分成两个部分,小于基准元素的放在左边,大于基准元素的放在右边。然后再递归地对左右两个部分进行快速排序。 ```java public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } } ``` 另一种常见的快速排序实现方法是使用栈。首先将初始的 low 和 high 压入栈中,然后循环处理栈顶的元素,直到栈为空。在每次循环中,先弹出栈顶的 low 和 high,并执行划分过程,将得到的两个新的 low 和 high 压入栈中。 ```java public class QuickSort { public static void quickSort(int[] arr, int low, int high) { Stack<Integer> stack = new Stack<>(); stack.push(low); stack.push(high); while (!stack.isEmpty()) { high = stack.pop(); low = stack.pop(); int pivot = partition(arr, low, high); if (low < pivot - 1) { stack.push(low); stack.push(pivot - 1); } if (pivot + 1 < high) { stack.push(pivot + 1); stack.push(high); } } } public static int partition(int[] arr, int low, int high) { int pivot = arr[low]; while (low < high) { while (low < high && arr[high] >= pivot) { high--; } arr[low] = arr[high]; while (low < high && arr[low] <= pivot) { low++; } arr[high] = arr[low]; } arr[low] = pivot; return low; } } ``` 这两种实现方法都基于相同的快速排序思想,只是在具体的实现上略有不同。快速排序的时间复杂度通常为 O(nlogn),是一种效率较高的排序算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值