bucket sort Implementation without using vector,pointer and counting sort
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
add a comment |
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
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
add a comment |
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
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
c sorting bucket-sort
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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]--;
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]--;
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
add a comment |
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]--;
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
add a comment |
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]--;
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]--;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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