BitSort

Правка en1, от Lincito_31, 2024-12-28 01:09:29

I found a new way to sort an array by using the bits(this only works for the languages that accept the use of bit). The idea is following:

Let's suppose that there are only 32_bit non-negative integers in our array. First we check the bit on position 30(not 31 since the bit 31 indicates whether the element is positive or not) for all elements in our array, if it is 0 then we push it back to an array called left, otherwise to array right. It is obvious that all elements in array left are smaller than any element in array right. So we only have to sort the array left and right separately and then just unite this two arrays and we get our original array sorted. In order to sort the array left, we do the same process but with one bit less(it is the same for the array right),i.e. with bit 29. Doing this process until we get to the bit on position 0 and we are done.

If you want to work with 64_bit integer you just have to change the initial position to 62 of the bits.

The code here:

void bitsort(vector<int>&a,int pos,int size){
	//Our base case:
	//When the size of this array is 1 or 0, then we don't have to sort it since it is already sorted.
	//When the position=-1, then we don't have to sort it since theres no position -1.
	if(size<2 || pos==-1){
		return;
	}
	//x=size of array left and y=size of array right
	int x=0,y=0;
	vector<int> left,right;
	for(int i=0;i<size;i++){
		//Checking if the bit on position pos is 1 or 0
		if(a[i]&(1<<pos)){
			right.push_back(a[i]);
			y++;
			}else{
			left.push_back(a[i]);
			x++;
		}
	}
	//sorting left and right independently
	bitsort(left,pos-1,x);
	bitsort(right,pos-1,y);
	//Joining these two arrays
	for(int i=0;i<x;i++){
		a[i]=left[i];
	}
	for(int i=0;i<y;i++){
		a[i+x]=right[i];
	}
	//So we have done
	return;
}
vector<int> sortArray(vector<int>& nums) {
	int n=nums.size();
	//Calling the function
	bitsort(nums,30,nums.size());
	return nums;
}

If we have negative numbers in our array, we have to do the same but creating an array for negative numbers and other for non-negative numbers. sorting them seperately and joining them we get our sorted array. The code:

//The bitsort function is the same
vector<int> sortArray(vector<int>& nums) {
	int n=nums.size();
	int x=0,y=0;
	vector<int> nega,posi;
        for(int i=0;i<n;i++){
		if(nums[i]<0){
			nega.push_back(nums[i]);
			x++;
		}else{
			posi.push_back(nums[i]);
			y++;
		}
	}
	bitsort(nega,30,x);
	bitsort(posi,30,y);
	for(int i=0;i<x;i++){
		nums[i]=nega[i];
	}
	for(int i=0;i<y;i++){
		nums[i+x]=posi[i];
	}
	return nums;
}

It works in approximately O(32n) or O(64n) depending on the elements we are working on(I don't write O(32n) as O(n) since it is approximately O(nlogn) and O(nlogn)!=O(n)).

I don't know if this sort is already posted somewhere, my friends told me that it is like radixsort. But as it is working with bits so I call it BitSort.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский Lincito_31 2024-12-28 01:09:29 3020 Initial revision (published)