bucket sort Implementation without using vector,pointer and counting sort










0















We want to use Bucket sort to sort numbers between 1 to 2001. the count of numbers can be 10E6.



I know the bucket sort algorithm. But the issue is that in this question, we are not permitted to use variable-length array, vector and pointer. (The only pointer related thing allowed is "pass by reference" of the array) The only solution I found is using using counting sort for each bucket, like the code below, so the code is more like counting sort than the bucket sort: (C language)



#include <stdio.h>
int buckets[201][10]=; int numbers[1000001]=;

void bucket_sort (int a,int n)
for (int i =0;i<=n-1;i++)

int index = a[i]/10, index2 = a[i]%10;
buckets[index][index2]++;

int counter =0;
for (int i =0;i<=200;i++)

for (int j =0; j<=9;j++)

while (buckets[i][j])

a[counter] = i*10+j;
counter++;
buckets[i][j]--;




int main()
int n;
scanf("%d",&n);
if (n==0)

return 0;

for (int i =0;i<=n-1;i++)

scanf("%d",&numbers[i]);
numbers[i];

bucket_sort(numbers,n);
for (int i =0;i<=n-1 ;i++)

printf("%dn", numbers[i]);

return 0;


I want to know can bucket sort be implemented without variable-length array, vector and pointer and also without counting sort. Probably using Insertion or Bubble sort. Note that it must be a reasonable bucket-sort algorithm. So defining very big buckets like int bucket [201][1000000]; is also an unacceptable approach.










share|improve this question






















  • what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

    – jenesaisquoi
    Nov 12 '18 at 21:47
















0















We want to use Bucket sort to sort numbers between 1 to 2001. the count of numbers can be 10E6.



I know the bucket sort algorithm. But the issue is that in this question, we are not permitted to use variable-length array, vector and pointer. (The only pointer related thing allowed is "pass by reference" of the array) The only solution I found is using using counting sort for each bucket, like the code below, so the code is more like counting sort than the bucket sort: (C language)



#include <stdio.h>
int buckets[201][10]=; int numbers[1000001]=;

void bucket_sort (int a,int n)
for (int i =0;i<=n-1;i++)

int index = a[i]/10, index2 = a[i]%10;
buckets[index][index2]++;

int counter =0;
for (int i =0;i<=200;i++)

for (int j =0; j<=9;j++)

while (buckets[i][j])

a[counter] = i*10+j;
counter++;
buckets[i][j]--;




int main()
int n;
scanf("%d",&n);
if (n==0)

return 0;

for (int i =0;i<=n-1;i++)

scanf("%d",&numbers[i]);
numbers[i];

bucket_sort(numbers,n);
for (int i =0;i<=n-1 ;i++)

printf("%dn", numbers[i]);

return 0;


I want to know can bucket sort be implemented without variable-length array, vector and pointer and also without counting sort. Probably using Insertion or Bubble sort. Note that it must be a reasonable bucket-sort algorithm. So defining very big buckets like int bucket [201][1000000]; is also an unacceptable approach.










share|improve this question






















  • what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

    – jenesaisquoi
    Nov 12 '18 at 21:47














0












0








0








We want to use Bucket sort to sort numbers between 1 to 2001. the count of numbers can be 10E6.



I know the bucket sort algorithm. But the issue is that in this question, we are not permitted to use variable-length array, vector and pointer. (The only pointer related thing allowed is "pass by reference" of the array) The only solution I found is using using counting sort for each bucket, like the code below, so the code is more like counting sort than the bucket sort: (C language)



#include <stdio.h>
int buckets[201][10]=; int numbers[1000001]=;

void bucket_sort (int a,int n)
for (int i =0;i<=n-1;i++)

int index = a[i]/10, index2 = a[i]%10;
buckets[index][index2]++;

int counter =0;
for (int i =0;i<=200;i++)

for (int j =0; j<=9;j++)

while (buckets[i][j])

a[counter] = i*10+j;
counter++;
buckets[i][j]--;




int main()
int n;
scanf("%d",&n);
if (n==0)

return 0;

for (int i =0;i<=n-1;i++)

scanf("%d",&numbers[i]);
numbers[i];

bucket_sort(numbers,n);
for (int i =0;i<=n-1 ;i++)

printf("%dn", numbers[i]);

return 0;


I want to know can bucket sort be implemented without variable-length array, vector and pointer and also without counting sort. Probably using Insertion or Bubble sort. Note that it must be a reasonable bucket-sort algorithm. So defining very big buckets like int bucket [201][1000000]; is also an unacceptable approach.










share|improve this question














We want to use Bucket sort to sort numbers between 1 to 2001. the count of numbers can be 10E6.



I know the bucket sort algorithm. But the issue is that in this question, we are not permitted to use variable-length array, vector and pointer. (The only pointer related thing allowed is "pass by reference" of the array) The only solution I found is using using counting sort for each bucket, like the code below, so the code is more like counting sort than the bucket sort: (C language)



#include <stdio.h>
int buckets[201][10]=; int numbers[1000001]=;

void bucket_sort (int a,int n)
for (int i =0;i<=n-1;i++)

int index = a[i]/10, index2 = a[i]%10;
buckets[index][index2]++;

int counter =0;
for (int i =0;i<=200;i++)

for (int j =0; j<=9;j++)

while (buckets[i][j])

a[counter] = i*10+j;
counter++;
buckets[i][j]--;




int main()
int n;
scanf("%d",&n);
if (n==0)

return 0;

for (int i =0;i<=n-1;i++)

scanf("%d",&numbers[i]);
numbers[i];

bucket_sort(numbers,n);
for (int i =0;i<=n-1 ;i++)

printf("%dn", numbers[i]);

return 0;


I want to know can bucket sort be implemented without variable-length array, vector and pointer and also without counting sort. Probably using Insertion or Bubble sort. Note that it must be a reasonable bucket-sort algorithm. So defining very big buckets like int bucket [201][1000000]; is also an unacceptable approach.







c sorting bucket-sort






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 21:11









amir naamir na

33




33












  • what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

    – jenesaisquoi
    Nov 12 '18 at 21:47


















  • what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

    – jenesaisquoi
    Nov 12 '18 at 21:47

















what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

– jenesaisquoi
Nov 12 '18 at 21:47






what about just computing the bucket an element belongs to. If the the bucket is full, either put the smallest element in the previous bucket or the largest in the next bucket, etc. If the elements are roughly uniformly distributed it doesn't seem so bad.

– jenesaisquoi
Nov 12 '18 at 21:47













1 Answer
1






active

oldest

votes


















0














Given that you can't use variable length arrays or pointers, one of which is required for a bucket sort, your best bet is to go with a counting sort. You only have 2000 possible values, so create an array of size 2000 and for each value you find increments the corresponding array element.



void counting_sort(int a, int n)

int count[2002] = 0 ;
int i, j;

for (i=0; i<n; i++)
count[a[i]]++;


for (i=0, j=0; i<n; i++)
while (!count[j])
j++;

a[i] = j;
count[j]--;







share|improve this answer























  • Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

    – amir na
    Nov 12 '18 at 21:28











  • Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

    – dbush
    Nov 12 '18 at 21:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270166%2fbucket-sort-implementation-without-using-vector-pointer-and-counting-sort%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Given that you can't use variable length arrays or pointers, one of which is required for a bucket sort, your best bet is to go with a counting sort. You only have 2000 possible values, so create an array of size 2000 and for each value you find increments the corresponding array element.



void counting_sort(int a, int n)

int count[2002] = 0 ;
int i, j;

for (i=0; i<n; i++)
count[a[i]]++;


for (i=0, j=0; i<n; i++)
while (!count[j])
j++;

a[i] = j;
count[j]--;







share|improve this answer























  • Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

    – amir na
    Nov 12 '18 at 21:28











  • Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

    – dbush
    Nov 12 '18 at 21:35















0














Given that you can't use variable length arrays or pointers, one of which is required for a bucket sort, your best bet is to go with a counting sort. You only have 2000 possible values, so create an array of size 2000 and for each value you find increments the corresponding array element.



void counting_sort(int a, int n)

int count[2002] = 0 ;
int i, j;

for (i=0; i<n; i++)
count[a[i]]++;


for (i=0, j=0; i<n; i++)
while (!count[j])
j++;

a[i] = j;
count[j]--;







share|improve this answer























  • Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

    – amir na
    Nov 12 '18 at 21:28











  • Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

    – dbush
    Nov 12 '18 at 21:35













0












0








0







Given that you can't use variable length arrays or pointers, one of which is required for a bucket sort, your best bet is to go with a counting sort. You only have 2000 possible values, so create an array of size 2000 and for each value you find increments the corresponding array element.



void counting_sort(int a, int n)

int count[2002] = 0 ;
int i, j;

for (i=0; i<n; i++)
count[a[i]]++;


for (i=0, j=0; i<n; i++)
while (!count[j])
j++;

a[i] = j;
count[j]--;







share|improve this answer













Given that you can't use variable length arrays or pointers, one of which is required for a bucket sort, your best bet is to go with a counting sort. You only have 2000 possible values, so create an array of size 2000 and for each value you find increments the corresponding array element.



void counting_sort(int a, int n)

int count[2002] = 0 ;
int i, j;

for (i=0; i<n; i++)
count[a[i]]++;


for (i=0, j=0; i<n; i++)
while (!count[j])
j++;

a[i] = j;
count[j]--;








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 21:23









dbushdbush

94.7k12101136




94.7k12101136












  • Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

    – amir na
    Nov 12 '18 at 21:28











  • Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

    – dbush
    Nov 12 '18 at 21:35

















  • Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

    – amir na
    Nov 12 '18 at 21:28











  • Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

    – dbush
    Nov 12 '18 at 21:35
















Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

– amir na
Nov 12 '18 at 21:28





Thanks. But as I said, my program is also a type of counting sort. I want to know whether any other sorting method exists that can be implemented with these conditions or not.

– amir na
Nov 12 '18 at 21:28













Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

– dbush
Nov 12 '18 at 21:35





Actually, what you have is a form of bucket sort. The buckets are the first 3 digits of the number in question, then each bucket uses a counting sort on the last digit. So technically is qualifies.

– dbush
Nov 12 '18 at 21:35

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270166%2fbucket-sort-implementation-without-using-vector-pointer-and-counting-sort%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo