Jineeshjohn’s Blog

Writing a bubble sort in JavaScript

Posted on: November 11, 2009

Hi,

Here i would like to show how to write a basic bubble sort using JavaScript.

 

function bubbleSort( arr ){
	var i = arr.length - 1, j;
	for (i ; i >= 0; i--){
		for (j = 0; j <= i; j++){
			if (arr[j+1] < arr[j]) {
				var temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
		}
	}
	return arr;
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


  • None
  • No comments yet

Categories

%d bloggers like this: