Hi there I was trying to make a varation of bucket sort. My Programm is easy because I'm a beginner in C but i don't know how print the 2 dim array when one row was realloc after putting in the numbers. I have tried int lenght= sizeof(buckets[0])/sizeof(buckets[0][0])
but then lenght=1
. My alternativ solution in the main and it is not very dynamic therefore can anybody tell me how to get the size of the row in a 2 dim array?? If I would add a number in the first bucket the bucket will be too small so i realloc the bucket but how can i print it now? Sorry for the bad english :)
#include <stdlib.h>
#include <stdio.h>
#define MAXZAHL 50
int compare(const void * a, const void * b){// compare for the qsort
return(*(int*)a - *(int*)b);
}
int** init_bucket(int l,int teile,int teilgr){// to init the buckets
int **bucket;
bucket=(int**)malloc(teile*sizeof(int));
for (int i=0;i<teile;i++){
bucket[i]=(int *)malloc(teilgr*sizeof(int));
}
for (int i=0;i<teile;i++){
for (int j=0;j<teilgr;j++){
bucket[i][j]=0;
printf("%3i",bucket[i][j]);
}
printf("\n");
}
return bucket;
}
void bucket_int(int**bucket,int bucketsize,int nextVal,int n){// to write the array in the buckets
int start=bucketsize;
for (int i=0;i<n;i++){
if(nextVal< start && bucket[0][i]==0){
bucket[0][i]=nextVal;
break;
}
if(nextVal< start && bucket[0][i]!=0&& i==n-1){
bucket[0][n]=(int)realloc(bucket,sizeof(int));
++i;
bucket[0][i]=nextVal;
break;
}
if((nextVal<(start*2)&& nextVal>=(start))&&(bucket[1][i]==0)){
bucket[1][i]=nextVal;
break;
}
if((nextVal<(start*3)&&nextVal>=(start*2))&&(bucket[2][i]==0)){
bucket[2][i]=nextVal;
break;
}
if((nextVal<(start*4)&&nextVal>=(start*3))&&(bucket[3][i]==0)){
bucket[3][i]=nextVal;
break;
}
if((nextVal<(start*5)&&nextVal>=(start*4))&&(bucket[4][i]==0)){
bucket[4][i]=nextVal;
break;
}
}
}
int main(){
int arr[]={3,26,2,10,33,45,20,15,11,9,34,40,19,16,4,5,26,49,1,0,6,8,7,3};
int len=sizeof(arr)/sizeof(int);//
int teile=5;
int teilgr=MAXZAHL/5;
int **buckets;
buckets=init_bucket(len,teile,teilgr);
for (int i=0;i<len;i++){//write the numbers in the buckets
bucket_int(buckets,teilgr,arr[i],len);
}
printf("\n");
//print Buckets
for (int i=0;i<teile;i++){
for (int j=0;j<teilgr;j++){// is there a way to say j<sizeof(buckets)??
printf("%3i",buckets[i][j]);
}
printf("\n");
}
//sorting the buckets with qsort
for (int i=0;i<teile;i++){
for (int j=0;j<teilgr;j++){
qsort(buckets[i],teilgr,sizeof(int**),compare);
}
}
printf("\n");
//print Buckets
for (int i=0;i<teile;i++){
for (int j=0;j<teilgr;j++){
printf("%3i",buckets[i][j]);
}
printf("\n");
}
printf("\n");
int f=0;
//Putting the buckets back to the array
for (int i=0;i<teile;i++){
for (int j=0;j<teilgr;j++){
if (buckets[i][j]!=0){
arr[f]=buckets[i][j];
f++;
}
}
}
printf("The sorted Array:\n");
for (int i=0;i<len-1;i++){
printf("%i ",arr[i]);
}
printf("\n");
return EXIT_SUCCESS;
}
how to get the size of the row
Read it from where you stored it, when you still knew it.
In C you cannot derive from a pointer to how much memory it points.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다