Write your question here.
I have to write a bucket sort and a merge sort using vector and lists, i think i did it but when i test me it gives me a segmentation fault and i dont know why.
Note that v.size() returns an unsigned integer type so if you subtract so that the result goes below zero the value will wrap around and become a very large value because unsigned integers can't store negative values.
bucket sort for values of 0 to 100:
unsigned int bucket[101];
for(all the data)
bucket[value]++
then when you recover the data,
for(all the buckets)
for(0 to bucket value) //say bucket value is 10 and you had 23 copies of the value 10
process(bucket ID) //eg, print 10, 23 times. the bucket array index is 10, and its value is 23
see?
there may be another version of it; I have seen several algorithms called 'bucket sort' but this is the one I recommend as is it is O(N) which is as fast as it gets to sort data. Note that you must know the value range of the data and it must be integer or integer like data to use this.
your version, with multiple values per bucket, is much more complex -- its slower and really has no niche where it is the best choice. you can magic the index to support negative values, eg
int buckets[202]; //adjust index into this to be value+100, so -100 becomes [0], -99 is 1, and so on. but the range still has to be known and continuous. a cute way to do this is to say int* bp = &buckets[101]; c++ will let you use bp[-100] so the basic logic is simplified with the pointer. You would use vectors here of course, I am just showing you the algorithm with arrays for simplicity
Dear friend,
Your Bucket sort is not bucket sort because you used your Bubbelsort function in it and we do not have that in Bucket sort.
Also your merg sort is false because will not sort the lists it just copy two lists after each other for example the forst list is [5,4,3,2,1] and second one is [6,7,9,8] the result would be [5,4,3,2,1,6,7,9,8]!!!!!