Minggu, 17 April 2016

4.5.3 HOW TO SOLVE

  No comments    
categories: 
pada bab ini  adalah program untuk mengetahui bilangan Array'


Algoritma :

void bucket_sort (int arr[], int n)
{
  //Here range is [1,100]
  int m = 101;

  //Create m empty buckets
  int buckets[m];

  //Intialize all buckets to 0
  for (int i = 0; i < m; ++i)
    buckets[i] = 0;

  //Increment the number of times each element is present in the input
  //array. Insert them in the buckets
  for (int i = 0; i < n; ++i)
    ++buckets[arr[i]];

  //Sort using insertion sort and concatenate
  for (int i = 0, j = 0; j < m; ++j)
    for (int k = buckets[j]; k > 0; --k)
      arr[i++] = j;void bucket_sort (int arr[], int n)
{
  //Here range is [1,100]
  int m = 101;

  //Create m empty buckets
  int buckets[m];

  //Intialize all buckets to 0
  for (int i = 0; i < m; ++i)
    buckets[i] = 0;

  //Increment the number of times each element is present in the input
  //array. Insert them in the buckets
  for (int i = 0; i < n; ++i)
    ++buckets[arr[i]];

  //Sort using insertion sort and concatenate
  for (int i = 0, j = 0; j < m; ++j)
    for (int k = buckets[j]; k > 0; --k)
      arr[i++] = j;


Berikut adalah program C++ dari penjelasan diatas:



Berikut adalah hasil compile dari program diatas:



0 komentar:

Posting Komentar