//Selection Sort
#include <iostream>
using namespace std;
void swap (int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
return;
}
int main()
{
int i, j, n;
cout << "Size of the Array : ";
cin >> n;
int a[n];
cout << "Elements of the Array : ";
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n - 1 ; i++)
for (j = i + 1; j < n; j++)
if(a[i] > a[j])
swap(&a[i], &a[j]);
cout << "Sorted Array : ";
for (i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
//Bubble Sort
#include <iostream>
using namespace std;
void swap (int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
return;
}
int main()
{
int i, j, n;
cout << "Size of the Array : ";
cin >> n;
int a[n];
cout << "Elements of the Array : ";
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < n - 1 ; i++)
for (j = 0; j < (n - 1) - i; j++)
if(a[j] > a[j + 1])
swap(&a[j], &a[j + 1]);
cout << "Sorted Array : ";
for (i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.