Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // implementation of bubble sorting algorithm
- // @author: fueanta
- // Date: Fri 7, Oct 2016
- #include "cstdio"
- template <typename T>
- void interchange(T &, T &);
- void asc(double*, int);
- void desc(double*, int);
- int main() {
- int sizeu; printf("Size of the array please: "); scanf("%d", &sizeu);
- double arr[sizeu];
- printf("\nNow, give the elements of the array:\n");
- for (int i= 0; i < sizeu; i++) {
- printf("\nElement at index no[%d]: ", i);
- scanf("%lf", &arr[i]);
- }
- printf("\n\nArray in ascending order.....\n");
- asc(arr, sizeu);
- for (int i = 0; i < sizeu ;i++) {
- printf("\nElement at index no[%d]: |%.2lf|\n", i, arr[i]);
- }
- printf("\n\nArray in descending order.....\n");
- desc(arr, sizeu);
- for (int i = 0; i < sizeu ;i++) {
- printf("\nElement at index no[%d]: |%.2lf|\n", i, arr[i]);
- }
- return 0;
- }
- template <typename T>
- void interchange(T &x, T &y) {
- T temp = x;
- x = y;
- y = temp;
- }
- void asc(double arr[], int n) {
- for (int i = 0; i < n-1 ; i++) {
- bool flag = false;
- for (int j = 0; j < n-1 ; j++) {
- if (arr[j] > arr[j+1]) {
- interchange(arr[j], arr[j+1]);
- flag = true;
- }
- }
- if (flag == false)
- break;
- }
- }
- void desc(double arr[], int n) {
- for (int i = 0; i < n-1 ; i++) {
- bool flag = false;
- for (int j = 0; j < n-1 ; j++) {
- if (arr[j] < arr[j+1]) {
- interchange(arr[j], arr[j+1]);
- flag = true;
- }
- }
- if (flag == false)
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment