Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement