Wondering about sorting algorithm

I was coding my own sorting algorithm, and when I ran it, I was unsure what category it would fall into (Eg: Bubble Sort, Selection Sort, and so on). When I ran the program to see individual patterns, I could find none. Could someone explain what category my algorithm is? Here is my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>


//Custom swap function
template<typename T> void swap(T &a, T &b)
{
	T temp = b;
	b = a;
	a = temp;
}


int main()
{
	int array[5];

	std::cout << "Enter Five Integers: ";

	std::cout << "\n";
	for (int k = 0; k < 5; k++)
	{
		std::cout << "[" << k+1 << "] ";
		std::cin >> array[k];
	}
	std::cin.ignore();
	std::cout << "Array before Sorting: ";

	for (int l = 0; l < 5; l++)
	{
		std::cout << "[" << array[l] << "]";
	}

	std::cout << "\n";

	for (int i = 0; i < 5; i++)
	{
		int currentIndex = i;

		for (int j = i + 1; j < 5; j++)
		{
			if (array[currentIndex] > array[j])
			{
				swap(array[currentIndex], array[j]);
				for (int m = 0; m < 5; m++)
				{
					std::cout << "[" << array[m] << "]";
				}
				std::cout << "\n";
			}
		}
	}

	std::cout << "Array After Sorting: ";

	for (int o = 0; o < 5; o++)
	{
		std::cout << "[" << array[o] << "]";
	}
	std::cout << "\n";
	std::cin.get();
	
}





Last edited on
> Could someone explain what category my algorithm is?

Bubble Sort

So you are here just to ask that? Is there a problem in your solution you are trying to deal with?
No, that was really it. And also, could you tell me how bubble sort works, because i must have misread about it since I already thought about that category?
closed account (48T7M4Gy)
Surely if yours is a bubble sort, and I don't doubt it is, you know what a bubble sort is Runner?


(What's does this look like? ... It looks like a sausage. ... What does a sausage look like? ... Oh *this? ... What does this look like? .....)
It is not a bubble sort.

This is a selection sort, without copy optimization.
(You copy every time you find a smaller value, instead of waiting to copy the smallest just once.)

Here's a pretty animation showing how it works:
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/
Ah, ok thanks Duoas, for the link. I get it now.

Annnd

@Kemort

...
closed account (48T7M4Gy)
?
Topic archived. No new replies allowed.