以线性时间复杂度对0、1和2的数组进行排序

Description:

描述:

Solution to the coding problem of sorting an array of 0's, 1's and 2's in linear time complexity.

解决以线性时间复杂度对0、1和2的数组进行排序的编码问题的解决方案。

Problem statement:

问题陈述:

Given an array consisting only 0's, 1's and 2's. Give an algorithm for sorting the array in O(n) time complexity ( in the sorted array, 0's will be at starting ,then the 1's & then the 2's).

给定一个仅包含0、1和2的数组。 给出一种以O(n)时间复杂度对数组进行排序的算法(在排序后的数组中,开始时为0,然后为1,然后为2)。

Solution:

解:

Algorithm

算法

  1. Set three variables low=0,mid=0, high=n-1 where n=length of input array

    设置三个变量low = 0,mid = 0,high = n-1 ,其中n =输入数组的长度

  2. While (mid<=high), consider three cases on basis of the value of array [mid].

    虽然(mid <= high) ,但要根据array [mid]的值考虑三种情况。

  3. If array[mid] is 0

    如果array [mid]为0

    Swap (

    交换(

    array [low], array [mid]) since 0's should be at starting

    array [low],array [mid] ),因为0应该在开始时

    Increment

    增量

    low

    Increment

    增量

    mid

    Break statement

    中断声明

  4. If array[mid] is 1

    如果array [mid]为1

    Keep as it is since 1's should be at middle after being sorted

    保持原样,因为排序后1应该位于中间

    Increment

    增量

    mid

    Break statement

    中断声明

  5. If array[mid] is 2

    如果array [mid]为2

    Swap (

    交换(

    array [mid], array [high])

    数组[mid],数组[high] )

    Decrement

    减量

    high

    Break statement

    中断声明

  6. Resulting array is sorted.

    结果数组已排序。

C ++程序以线性时间复杂度对0、1和2的数组进行排序 (C++ program to sort an array of 0's, 1's and 2's in linear time complexity)

#include<stdio.h>
#include<stdlib.h>

//function to swap by reference
void swap(int*a,int*b){
	int temp;
	temp=*b;
	*b=*a;
	*a=temp;
	return;
}
int* asort(int *a,int n){
	int low=0,mid=0,high=n-1;   //variables are set
	
	while(mid<=high){
		switch(a[mid]){
			case 0:     //if a[mid]==0
				//swap a[low] & a[mid], swapping by reference
				swap(&a[low],&a[mid]);  
				low++;      //increment low
				mid++;      //increment mid
				break;
			case 1:     //if a[mid]==1
				mid++;      //increment mid
				break;
			case 2:     //if a[mid]==2
				//swap a[mid] & a[high], swapping by reference
				swap(&a[mid],&a[high]);  
				high--;     //decrement high
				break;
		}
	}
	//returning adress of array(sorted)
	return a;   
}

int main() {
	int n;

	printf("enter no of array elements\n");
	//input array length
	scanf("%d",&n);

	int* a=(int*)malloc(sizeof(int)*n);

	printf("enter array elements\n");
	//input array elements
	for(int j=0;j<n;j++)      
		scanf("%d",&a[j]);
	//array is modified
	a=asort(a,n);   
	printf("after being sorted..............\n");
	//printing the sorted array
	for(int j=0;j<n-1;j++)   
		printf("%d ",a[j]);
	
	printf("%d\n",a[n-1]);

	return 0;
}

Output

输出量

First run:
enter no of array elements 
10 
enter array elements 
0
2
2
1
1
1
2
1
2
1
after being sorted.............. 
0 1 1 1 1 1 2 2 2 2 


Second run:
enter no of array elements 
10 
enter array elements 
2
2
2
2
0
0
0
0
1
1
after being sorted.............. 
0 0 0 0 1 1 2 2 2 2

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/icp/sort-an-array-of-0s-1s-and-2s-in-linear-time-complexity.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值