create account

Easy Tutorial: Computer Programming for DUMMIES (sorting) by charlie.wilson

View this thread on: hive.blogpeakd.comecency.com
· @charlie.wilson ·
$63.58
Easy Tutorial: Computer Programming for DUMMIES (sorting)
http://eskipaper.com/images/binary-2.jpg

Hey guys! Here is another basic programming tutorial. This tutorial is about sorting arrays. We can sort numbers in ascending or descending order. The algorithm I will show you is pretty simple. I will be coding in C++, but the algorithm will still apply to other languages. This is part eight of my tutorials. If you want to catch up on my previous tutorials, here they are:

[**Part 1: Hello World!**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming)

[**Part 2: Variables**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies)

[**Part 3: Functions**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies)

[**Part 4: if(), else, else if()**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners)

[**Part 5: Loops**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly)

[**Part 6: Arrays**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly)

[**Part 7: Basic Input/Output**](https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly)

# Bubble sort
This is probably the simplest sort algorithm. It compares two numbers in an array at a time, starting with the first two. If the first number is bigger than the second, then they swap positions. It then compares the second number to the third number, and so on... Once the last two numbers are compared, the largest value will be at the end. The sort then starts all over, but ignores the last number. Here is an example:
1. **8** **3** 6 1 -- 8 is bigger than 3, swap
2. 3 **8** **6** 1 -- 8 is bigger than 6, swap
3. 3 6 **8** **1** -- 8 is bigger than 1, swap
4. 3 6 1 - 8 -- 8 is now in the right position
5. **3** **6** 1 - 8 -- 3 is smaller than 6, don't swap
6. 3 **6** **1** - 8 -- 6 is bigger than 1, swap
7. 3 1 - 6 8 -- 6 is now in the right position
8. **3** **1** - 6 8 -- 3 is bigger than 1, swap
9. 1 3 6 8 -- They are in the correct order.

Here is a gif from Wikipedia:
https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif

Here is how you implement the bubble sort in C++:
arr[]: the array being sorted
arrSize: (integer) the number of elements in the array
```
for(int x = 0; x < arrSize; x++)
{
	for(int y = 0; y < arrSize-x-1; y++)
	{
		if(arr[y] > arr[y + 1])
		{
			int temp = arr[y];
			arr[y] = arr[y + 1];
			arr[y + 1] = temp;
		}
	}
}
```
I realize that nested for() loops may be a bit confusing at first, so let's walk through this. The first for loop represents every occasion the sort goes from the beginning to the end of the array (remember, it starts over until all of the elements are sorted -- this may mean that it swaps elements until there are only two elements left to swap). Each iteration of this loop in my example would be numbers 1-4, 5-7, and 8-9 (3 iterations). The second for() loop represents comparing each to the next. Look at "y < arrSize-x-1." The "-x" is there because after each iteration, the last comparison is unnecessary. "x" increments by one every iteration, so subtracting "x" will keep one extra comparison from happening each iteration. Also, to swap values, you need a temporary variable to hold one value so that it doesn't get lost. That is what "temp" is for. If you want to sort in descending order, you just have to change the greater than sign to a less than sign on line five.

Here is a program I wrote just to apply this sort to a working example:
```
#include<iostream>
using namespace std;

void bubble_ascending(int arr[], int arrSize)
{
	for(int x = 0; x < arrSize; x++)
	{
		for(int y = 0; y < arrSize-x-1; y++)
		{
			if(arr[y] > arr[y + 1])
			{
				int temp = arr[y];
				arr[y] = arr[y + 1];
				arr[y + 1] = temp;
			}
		}
	}
}

void bubble_descending(int arr[], int arrSize)
{
	for(int x = 0; x < arrSize; x++)
	{
		for(int y = 0; y < arrSize-x-1; y++)
		{
			if(arr[y] < arr[y + 1])
			{
				int temp = arr[y];
				arr[y] = arr[y + 1];
				arr[y + 1] = temp;
			}
		}
	}
}		

int main()
{
	int arr[10];
	cout << "Enter 10 integers: ";
	for(int x = 0; x < 10; x++)
		cin >> arr[x];

	bubble_ascending(arr, 10);
	cout << "Ascending order: ";
	for(int x = 0; x < 10; x++)
		cout << arr[x] << " ";
	cout << endl;
	
	bubble_descending(arr, 10);
	cout << "Descending order: ";
	for(int x = 0; x < 10; x++)
		cout << arr[x] << " ";
	cout << endl;
	
	return 0;
}
```
Here is the output (user input in bold):
______________________
[cmw4026@omega test]$ **g++ sort.cpp**
[cmw4026@omega test]$ **./a.out**
Enter 10 integers: **6 3 56 98 12 56 309 21 555 -8**
Ascending order: -8 3 6 12 21 56 56 98 309 555
Descending order: 555 309 98 56 56 21 12 6 3 -8
[cmw4026@omega test]$
______________________
The parameters for each function is simply the array to be sorted, then the number of elements in the array.

I hope this was useful! Bubble sort is not the fastest sort algorithm, but is is probably the easiest. It is fine because speed will not matter unless you are sorting a HUGE data set! Leave a comment if you have any suggestions, or want a faster algorithm.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorcharlie.wilson
permlinkeasy-tutorial-computer-programming-for-dummies-sorting
categoryprogramming
json_metadata{"tags":["programming","tutorial","howto","sort","arrays"],"image":["http://eskipaper.com/images/binary-2.jpg","https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif"],"links":["https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-2-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-part-3-for-dummies","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-very-helpful-for-beginners","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-not-language-specific-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-beginner-friendly","https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming-for-dummies-p7-very-beginner-friendly"]}
created2016-09-29 21:51:27
last_update2016-09-29 21:51:27
depth0
children2
last_payout2016-10-31 00:17:00
cashout_time1969-12-31 23:59:59
total_payout_value48.668 HBD
curator_payout_value14.907 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,402
author_reputation18,779,183,062,076
root_title"Easy Tutorial: Computer Programming for DUMMIES (sorting)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,399,113
net_rshares36,311,112,588,505
author_curate_reward""
vote details (44)
@electrowarrior · (edited)
Elecrowarrior here thanks for the invite. I like to program in c and c++. Usually my Arduino robots are my thing although I'm still a novice. Thank you. P.s.  followed
👍  ,
properties (23)
authorelectrowarrior
permlinkre-charliewilson-easy-tutorial-computer-programming-for-dummies-sorting-20160930t212434781z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-09-30 21:24:39
last_update2016-09-30 21:25:12
depth1
children1
last_payout2016-10-31 00:17:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length167
author_reputation4,624,364,028,471
root_title"Easy Tutorial: Computer Programming for DUMMIES (sorting)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,408,192
net_rshares4,055,621,494
author_curate_reward""
vote details (2)
@charlie.wilson ·
Thanks a lot!
properties (22)
authorcharlie.wilson
permlinkre-electrowarrior-re-charliewilson-easy-tutorial-computer-programming-for-dummies-sorting-20160930t232425211z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-09-30 23:24:27
last_update2016-09-30 23:24:27
depth2
children0
last_payout2016-10-31 00:17:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation18,779,183,062,076
root_title"Easy Tutorial: Computer Programming for DUMMIES (sorting)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,409,024
net_rshares0