Sort 排序的几种用法

 

PS: 之前给新生们写的。在这里也发一下吧。

 

====================================================================================

 

【整数数组】直接从小到大排序 

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从小到大顺序输出数组 

int main(){
	int i,n,a[200];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	for(i=0;i<n;i++)
		printf("%d\n",a[i]);
	return 0;
}

 

======================================================================================

 

【整数数组】从小到大排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从小到大顺序输出数组 

int cmp(int x,int y){
	return x<y;
}

int main(){
	int i,n,a[200];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%d ",a[i]);
	puts("");
	return 0;
}

 

==================================================================================

 

【整数数组】从大到小排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从大到小顺序输出数组 

int cmp(int x,int y){
	return x>y;
}

int main(){
	int i,n,a[200];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%d ",a[i]);
	puts("");
	return 0;
}


======================================================================================

【结构体数组】简单排序 

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后再输入n个整数,表示结构体的其中的一个属性q 
//输出
//    根据结构体数组的属性q的从大到小顺序,输出结构体数组 
struct S{
	int q;
	int b;
}a[200];

int cmp(S x,S y){
	return x.q>y.q;
}

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&a[i].q);
		a[i].b=i;
	}
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%d 原位置:%d\n",a[i].q,a[i].b);
	puts("");
	return 0;
}

 


==============================================================================

 

【结构体数组】双属性排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后接下去n行,每行输入两个整数,表示结构体的其中的两个属性p和q 
//输出
//    根据p属性的大小进行从大到小排序,在p相等的情况下,则根据q的大小进行从大到小排序 

/*
样例输入:
5
3 5
4 7
6 3
4 9
3 2 

*/ 

struct S{
	int q;
	int p;
}a[200];

int cmp(S x,S y){
	if(x.p==y.p)
		return x.q>y.q;
	return x.p>y.p;
}

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&a[i].p,&a[i].q);
	sort(a,a+n,cmp);
	puts("排序后:"); 
	for(i=0;i<n;i++)
		printf("%d %d\n",a[i].p,a[i].q);
	puts("");
	return 0;
}

 

==============================================================================

 

【结构体数组】双属性排序2    //   所有的结构体排序都可以把以上的cmp函数,写成结构体运算符重载operator<

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后接下去n行,每行输入两个整数,表示结构体的其中的两个属性p和q 
//输出
//    根据p属性的大小进行从大到小排序,在p相等的情况下,则根据q的大小进行从大到小排序 

/*
样例输入:
5
3 5
4 7
6 3
4 9
3 2 

*/ 

struct S{
	int q;
	int p;
	bool friend operator <(S a,S b){
		if(a.p==b.p) return a.q>b.q;
		return a.p>b.p;
	}
}a[200];

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&a[i].p,&a[i].q);
	sort(a,a+n);
	puts("排序后:"); 
	for(i=0;i<n;i++)
		printf("%d %d\n",a[i].p,a[i].q);
	puts("");
	return 0;
}

=================================================================================================

 

【字符串数组】排序(char字符串)

 

#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

//输入: 
//    先输入字符串数组长度 n
//    然后再输入n个字符串 
//输出
//    从小到大顺序输出字符串数组 

char as[200][50];

int cmp(int x,int y){
	return strcmp(as[x],as[y])==-1;
} 

int main(){
	int i,n,a[200];
	
	scanf("%d",&n);
	for(i=0;i<n;i++){  // 注意字符串数组as[] 顺序并没有改变,改变的是存储 字符串数组as[] 映射 的数组a[] 
		scanf("%s",as[i]);
		a[i]=i;
	}
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%s\n",as[a[i]]);
	return 0;
}

 

=================================================================================================

 

【字符串数组】排序(string 字符串)

#include<algorithm>
#include<iostream>

using namespace std;

//输入: 
//    先输入字符串数组长度 n
//    然后再输入n个字符串 
//输出
//    从小到大顺序输出字符串数组 

string as[100];

int main(){
	int i,n,a[200];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		cin>>as[i];
	sort(as,as+n);
	cout<<"排序后:"<<endl;
	for(i=0;i<n;i++)
		cout<<as[i]<<endl;
	return 0;
}


 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值