# bubble sort in CUDA

**URL:** <https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392>\
**Category:** CUDA Programming and Performance\
**Created:** [May 11, 2011, 3:33pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392 "2011-05-11T15:33:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![acudalearner](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@acudalearner](https://forums.developer.nvidia.com/u/acudalearner)\
**Post date:** [May 11, 2011, 3:33pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/1 "2011-05-11T15:33:41Z")

</div>

hi,  
i need the implementation of bubble sort in CUDA .could you help me please?  
your help will be appreciated.

---

<div class="post-metadata">

**Author:** ![kbam](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@kbam](https://forums.developer.nvidia.com/u/kbam)\
**Post date:** [May 12, 2011, 12:48am UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/2 "2011-05-12T00:48:14Z")

</div>

OK for bubble I expect you have an array that is small, i.e. a lot less than 1000 elements.  
I suggest…  
supposing your array is 64 elements long, then assign 32 threads,  
for each thread N = 2\* its\_thread\_number  
on each go round a loop each thread should do this  
if ( A[N] \< A[N+1] ) swap(A[N],A[N+1] )  
if ( A[N+1] \< A[N+2] ) swap(A[N+1],A[N+2] )

Why have N = 2_threadNumber?  
Reason is that in cuda all threads in a warp do the same instruction in a single instruction cycle.  
so thread 0 will be swapping A[0] with A[1] at the same time as thread 1 swaps A[2] and A[3]  
N=2_threadNumber stops them from both trying to update contents of a cell at same time.

Probably simplest to stop after length\_of\_array iterations or is it length\_of\_array/2 up to you to work that out.

You will also have to work out what \_\_synchthreads() are needed if the array can be \>64 long.  
and handle case where array is not an even number of elements. e.g. what if array is 13 long ?

And there may be better ways.

---

<div class="post-metadata">

**Author:** ![apostglen46](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@apostglen46](https://forums.developer.nvidia.com/u/apostglen46)\
**Post date:** [May 12, 2011, 5:14am UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/3 "2011-05-12T05:14:36Z")

</div>

shouldn’t bitonic sort be faster even for very small inputs?

---

<div class="post-metadata">

**Author:** ![monsh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@monsh](https://forums.developer.nvidia.com/u/monsh)\
**Post date:** [May 13, 2011, 4:54pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/4 "2011-05-13T16:54:37Z")

</div>

Would u provide the program please

Im doing a lil research on sorting algorithm implementation on CUDA .

doesn’t have to be fast, just meaningful result that’s all .

thanks ^^,

---

<div class="post-metadata">

**Author:** ![tera](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tera](https://forums.developer.nvidia.com/u/tera)\
**Post date:** [May 13, 2011, 6:54pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/5 "2011-05-13T18:54:40Z")

</div>

The purpose of this forum is to help with CUDA problems, not to do your assignments for you. [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") Most of the regulars here have their own projects to work on.

---

<div class="post-metadata">

**Author:** ![monsh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@monsh](https://forums.developer.nvidia.com/u/monsh)\
**Post date:** [May 13, 2011, 10:44pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/6 "2011-05-13T22:44:10Z")

</div>

I just need for some reference .  
I’ve got my stuffs going on already .  
Code optimization is what i need for now .  
a lil help for reference would be greatly appreciated .

thanks anyway =)

---

<div class="post-metadata">

**Author:** ![jjp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@jjp](https://forums.developer.nvidia.com/u/jjp)\
**Post date:** [May 14, 2011, 9:58pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/7 "2011-05-14T21:58:06Z")

</div>

There is plenty of Cuda sorting code available. Take a look at Thrust, for example.

---

<div class="post-metadata">

**Author:** ![monsh](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@monsh](https://forums.developer.nvidia.com/u/monsh)\
**Post date:** [June 11, 2011, 9:48pm UTC](https://forums.developer.nvidia.com/t/bubble-sort-in-cuda/22392/8 "2011-06-11T21:48:06Z")

</div>

thanks for the tips bro
