📜  存储桶排序以对带有负数的数组进行排序

📅  最后修改于: 2021-05-07 10:05:18             🧑  作者: Mango

我们已经在Bucket Sort的主要文章中讨论了bucket sort。
当输入在一个范围内均匀分布时,存储桶排序主要有用。例如,考虑对一组大的浮点数进行排序的问题,这些浮点数的范围是0.0到1.0,并且在整个范围内均匀分布。在以上文章中,我们讨论了存储桶排序以对大于零的数字进行排序。
如何修改存储桶排序以对正数和负数进行排序?
例子:

Input : arr[] = { -0.897, 0.565, 0.656, -0.1234, 0, 0.3434 } 
Output : -0.897 -0.1234  0 0.3434 0.565 0.656 

在这里,我们考虑数字在-1.0到1.0的范围内(浮点数)
算法 :

sortMixed(arr[], n)
1) Split array into two parts 
   create two Empty vector Neg[], Pos[] 
   (for negative and positive element respectively)
   Store all negative element in Neg[] by converting
   into positive (Neg[i] = -1 * Arr[i] )
   Store all +ve in pos[]  (pos[i] =  Arr[i])
2) Call function bucketSortPositive(Pos, pos.size())
   Call function bucketSortPositive(Neg, Neg.size())

bucketSortPositive(arr[], n)
3) Create n empty buckets (Or lists).
4) Do following for every array element arr[i]. 
       a) Insert arr[i] into bucket[n*array[i]]
5) Sort individual buckets using insertion sort.
6) Concatenate all sorted buckets. 

下面是上述想法的实现(对于浮点数)

CPP
// C++ program to sort an array of positive
// and negative numbers using bucket sort
#include 
using namespace std;
 
// Function to sort arr[] of size n using
// bucket sort
void bucketSort(vector &arr, int n)
{
    // 1) Create n empty buckets
    vector b[n];
 
    // 2) Put array elements in different
    //    buckets
    for (int i=0; iNeg ;
    vectorPos;
 
    // traverse array elements
    for (int i=0; i


Java
// Java program to sort an array of positive
// and negative numbers using bucket sort
import java.util.*;
class GFG
{
 
  // Function to sort arr[] of size n using
  // bucket sort
  static void bucketSort(Vector arr, int n)
  {
 
    // 1) Create n empty buckets
    @SuppressWarnings("unchecked")
    Vector b[] = new Vector[n];
    for (int i = 0; i < b.length; i++)
      b[i] = new Vector();
 
    // 2) Put array elements in different
    // buckets
    for (int i = 0; i < n; i++)
    {
      int bi = (int)(n*arr.get(i)); // Index in bucket
      b[bi].add(arr.get(i));
    }
 
    // 3) Sort individual buckets
    for (int i = 0; i < n; i++)
      Collections.sort(b[i]);
 
    // 4) Concatenate all buckets into arr[]
    int index = 0;
    arr.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < b[i].size(); j++)
        arr.add(b[i].get(j));
  }
 
  // This function mainly slpits array into two
  // and then calls bucketSort() for two arrays.
  static void sortMixed(double arr[], int n)
  {
    VectorNeg = new Vector<>();
    VectorPos = new Vector<>(); 
 
    // traverse array elements
    for (int i = 0; i < n; i++)
    {
      if (arr[i] < 0)
 
        // store -Ve elements by
        // converting into +ve element
        Neg.add (-1 * arr[i]) ;
      else
 
        // store +ve elements
        Pos.add (arr[i]) ;
    }
    bucketSort(Neg, (int)Neg.size());
    bucketSort(Pos, (int)Pos.size());
 
    // First store elements of Neg[] array
    // by converting into -ve
    for (int i = 0; i < Neg.size(); i++)
      arr[i] = -1 * Neg.get( Neg.size() -1 - i);
 
    // store +ve element
    for(int j = Neg.size(); j < n; j++)
      arr[j] = Pos.get(j - Neg.size());
  }
 
  /* Driver program to test above function */
  public static void main(String[] args)
  {
    double arr[] = {-0.897, 0.565, 0.656,
                    -0.1234, 0, 0.3434};
    int n = arr.length;
    sortMixed(arr, n);
 
    System.out.print("Sorted array is \n");
    for (int i = 0; i < n; i++)
      System.out.print(arr[i] + " ");
  }
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to sort an array of positive
// and negative numbers using bucket sort
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to sort []arr of size n using
  // bucket sort
  static void bucketSort(List arr, int n)
  {
 
    // 1) Create n empty buckets
 
    List []b = new List[n];
    for (int i = 0; i < b.Length; i++)
      b[i] = new List();
 
    // 2) Put array elements in different
    // buckets
    for (int i = 0; i < n; i++)
    {
      int bi = (int)(n*arr[i]); // Index in bucket
      b[bi].Add(arr[i]);
    }
 
    // 3) Sort individual buckets
    for (int i = 0; i < n; i++)
      b[i].Sort();
 
    // 4) Concatenate all buckets into []arr
    int index = 0;
    arr.Clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < b[i].Count; j++)
        arr.Add(b[i][j]);
  }
 
  // This function mainly slpits array into two
  // and then calls bucketSort() for two arrays.
  static void sortMixed(double []arr, int n)
  {
    ListNeg = new List();
    ListPos = new List(); 
 
    // traverse array elements
    for (int i = 0; i < n; i++)
    {
      if (arr[i] < 0)
 
        // store -Ve elements by
        // converting into +ve element
        Neg.Add (-1 * arr[i]) ;
      else
 
        // store +ve elements
        Pos.Add (arr[i]) ;
    }
    bucketSort(Neg, (int)Neg.Count);
    bucketSort(Pos, (int)Pos.Count);
 
    // First store elements of Neg[] array
    // by converting into -ve
    for (int i = 0; i < Neg.Count; i++)
      arr[i] = -1 * Neg[ Neg.Count -1 - i];
 
    // store +ve element
    for(int j = Neg.Count; j < n; j++)
      arr[j] = Pos[j - Neg.Count];
  }
 
  /* Driver program to test above function */
  public static void Main(String[] args)
  {
    double []arr = {-0.897, 0.565, 0.656,
                    -0.1234, 0, 0.3434};
    int n = arr.Length;
    sortMixed(arr, n);
 
    Console.Write("Sorted array is \n");
    for (int i = 0; i < n; i++)
      Console.Write(arr[i] + " ");
  }
}
 
// This code is contributed by Rajput-Ji


输出:

Sorted array is 
-0.897  -0.1234 0 0.3434 0.565 0.656