Question

In: Computer Science

Could the sorting algorithm start out as if then else situation?

Could the sorting algorithm start out as if then else situation?

Solutions

Expert Solution

Yes we can use if else to sort

example a program sorting three number using if and else condition

//C++ program

#include<iostream>
using namespace std;

int main(){
   int a,b,c;
  
   cout<<"Enter three numbers : ";
   cin>>a>>b>>c;
  
   if (a < b) {
       if (a < c){
           if (b < c) cout<<a<<" "<<b<<" "<<c<<endl;
           else cout<<a<<" "<<c<<" "<<b<<endl;
       }
       else cout<<c<<" "<<a<<" "<<b<<endl;

   }
   else{
       if (b < c) {
           if (a < c)
               cout<<b<<" "<<a<<" "<<c<<endl;
           else
               cout<<b<<" "<<c<<" "<<a<<endl;
       }
       else
           cout<<c<<" "<<b<<" "<<a<<endl;   
   }
   return 0;

}

//sample output


Related Solutions

1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric? (b) Consider two algorithms: f(n) and g(n). You run...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements...
Recall the definition of stable sorting: a sorting algorithm is said to be stable when elements with equal keys (after the sorting is complete) remain in the same order as they were in the input (before the sorting). Answer the following: Is Radix Sort stable, and why? Show an example: start from 10 random integer numbers (of at least 2 digits per integer number) to be sorted, where at least 2 of those 10 elements need to have equal keys;...
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm....
-----xxxxx-------Could you please use java language. thank you. :::::: XXXX::::::::::: Implement a recursive reverse sorting algorithm. The following requirements should meet: a The program shall graphically prompt the user for a file. bThe program shall read the selected file which will contain 1 integer per line. c. The program shall sort the values it reads from the file from largest to smallest. d.The program shall write the values to an output file from largest to smallest in the same directory...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Could someone point out the logical error in this code? The code seems to start at...
Could someone point out the logical error in this code? The code seems to start at the last input. But I want the code to output something like this: Enter name: A Enter time:5 Enter name:B Enter time: 4 Enter name: C Enter time:3 Enter name:D Enter time: 9 Enter name: E Enter time: 11 A process is started and ends in 5 sec. 1 sec passed... 2 sec passed... 3 sec passed... 4 sec passed... 5 sec passed... B...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide...
Consider a sorting algorithm that combines merge sort and insertion sort algorithm. We still use divide and conquer like merge sort, however when the number of elements in an array is at most k elements (k is a parameter), we stop dividing the elements as the regular merge sort, instead, we call the insertion sort. Assuming we have defined the following two procedures: insertion-sort(A[p..q]) which sort the subarray A[p..q] merge(A[p,q,r]) which merges the sorted subarray A[p..r] and A[r+1..q] Try to...
Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection...
Sorting algorithm for arrays: understand how to perform the following algorithms. (a). Simple sorting Bubblesort:T(n)ÎO(n2) Selection sort : T(n) Î O(n2) Insertion sort: T(n) Î O(n2) (b).Advanced sorting i. Shell sort: O(n2) < O(n1.25) < O(nlogn), O(n1.25) is empirical result for shell sort. Merge sort: T(n) Î O(nlogn), need one temporary array with the same length as the array needed to be sorted. Quick sort: -average case: T(n) Î O(nlogn), -worst case(rare occurrence): T(n) Î O(n2) 5. Searching algorithm for...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT