Quiz 13: Oop and Adts

Computing

Program Plan: • Include the required header files into the program. • Define a function named "doubleendSSort()" with two parameters. o Declare required variables. o Compare the positions of an array by using the "while" loop. • Scan the first half of the element and moves the smallest element to the first position of the array. • Scan the second half of the element and moves the largest element to the last position of the array. • Display the array "a". • Define the "main()" function to perform the double end selection sort. o Declare the required variables. o Call the function "doubleendSSort()" by passing the array to perform the double end selection sort. Program code: / ********************************************************** * Program to sort an array using double end selection sort* ********************************************************** / / / Include required header #include "stdafx.h" #include using namespace std; / / function used to sort an array void doubleendSSort(int a[], int n) { / / Declare variables int i = 0,ind, swap, low, high, ind1, k = n - 1; / / compare the position of an array while (i k) { / / assign first element as samll low = a[i]; / / assign the position of first value ind = i; / / assign last element as large high = a[k]; / / assign the position of last value ind1 = k; / / loop used to scan the array for (int j = i + 1;j { / / pick the value which is lower than small if (low a[j]) { / / if it find then make it as samll low = a[j]; / / assign the new small value position ind = j; } } / / check the poisiton whether it is same as i if (ind != i) { / / interchange the small value swap = a[i]; a[i] = a[ind]; a[ind] = swap; } / / loop used to scan the array for (int j = k - 1;j i;j--) { / / pick the value which is larger than high if (high { / / if it find then make it as high high = a[j]; / / assign the new high value position ind1 = j; } } / / check the poisiton whether it is same as k if (ind1 != k) { / / interchange the largest value swap = a[k]; a[k] = a[ind1]; a[ind1] = swap; } / / print the array for (int i = 0;i { cout a[i] " "; } cout "\n"; / / increment i value i++; / / decrement k value k--; } } / / Function main int main() { / / assign the values to an array int a[6] = { 30,50,70,10,40,60 },n=6; / / call double-ended selection function for sorting doubleendSSort(a, n); / / Pause the screen system("pause"); / / return 0 return 0; } Sample Ou tput: 10 50 60 30 40 70 10 30 40 50 60 70 10 30 40 50 60 70

Program Plan: • Include the required header files into the program. • Define a function named "minMaxSort()" with two parameters. o Declare required variables. o Compare the positions of an array by using the "while" loop. • Find and move the smallest element in the list to the first half of the list. • Find and move the largest element in the list to the second half of the list. • Display the array "a". • Scan the first half of the element and moves the smallest element to the first position of the array. • Scan the second half of the element and moves the largest element to the last position of the array. • Display the array "a". • Define the "main()" function to perform the double end selection sort. o Declare the required variables. o Call the function "minMaxSort()" by passing the array to perform the double end selection sort. Program code: / ********************************************************** * Program to sort an array using double end selection sort* ********************************************************** / / / Include required header files #include "stdafx.h" #include using namespace std; / / function used to sort an array void minMaxSort(int a[], int n) { / / Declare variables int i = 0, ind, swap, low, high, ind1, k = n - 1, swap1; / / compare the position of an array while (i k) { / / loop up to n / 2 times for (int h = 0; h n / 2; h++) { / * check first half values with second half values * / if (a[h] a[n - 1 - h]) { / / Interchange swap1 = a[h]; a[h] = a[n - 1 - h]; a[n - 1 - h] = swap1; } } cout "\nRainbow Pattern:"; / / Display the values array a for (int h = 0; h n;h++) { cout a[h] " "; } cout "\n"; / / assign first element as samll low = a[i]; / / assign the position of first value ind = i; / / assign last element as large high = a[k]; / / assign the position of last value ind1 = k; / / loop used to scan the array for (int j = i + 1; j n / 2; j++) { / / pick the value which is lower than small if (low a[j]) { / / if it find then make it as samll low = a[j]; / / assign the new small value position ind = j; } } / / check the poisiton whether it is same as i if (ind != i) { / / interchange the small value swap = a[i]; a[i] = a[ind]; a[ind] = swap; } / / loop used to scan the array for (int j = k - 1; j = n / 2; j--) { / / pick the value which is larger than high if (high a[j]) { / / if it find then make it as high high = a[j]; / / assign the new high value position ind1 = j; } } / / check the poisiton whether it is same as k if (ind1 != k) { / / interchange the largest value swap = a[k]; a[k] = a[ind1]; a[ind1] = swap; } / / print the array cout "\nselection sort"; for (int i = 0;i { cout a[i] " "; } cout "\n"; / / increment i value i++; / / decrement k value k--; } } / / Function main int main() { / / assign the values to an array int a[10] = { 30,80,90,20,60,70,10,100}, n = 8; / / call double-ended selection function for sorting minMaxSort(a, n); / / pause the screen system("pause"); / / return 0 return 0; } Sample output: Rainbow Pattern:30 10 70 20 60 90 80 100 selection sort10 30 70 20 60 90 80 100 Rainbow Pattern:10 30 70 20 60 90 80 100 selection sort10 20 70 30 60 80 90 100 Rainbow Pattern:10 20 70 30 60 80 90 100 selection sort10 20 30 70 60 80 90 100 Rainbow Pattern:10 20 30 60 70 80 90 100 selection sort10 20 30 60 70 80 90 100

Program plan: • Include header files • Call function "Recursiveselectsort()" to pass the integer variables "arr[]", "sindex" and "len". o Declare the integer variable "list", "sval", "temval", "m". o Using "for" loop to count the random numbers. o Return a random integral number in the range between "0" and "rand()" value maximum. o Print before sorted elements o Using if loop to check the condition of the random elements • Call the recursive selection sort function "Recursiveselectsort()" o Print the after sorted elements o Using for loop to check the condition of the random number and length values. o Return the elements. • Inline function call "Recursiveselectsort()" function to pass the integer variables "arr[]", "sindex" and "len". o If loop checks "index = len-1" o Return start index value o Using for loop to check the position of the index element and increment the index value o If loop check (arr[index1] o Store the minimum index to the index value o Swap the index value into the array list • Call recursive selection sort function "RecursiveselectSort(arr, sindex + 1,len)" to print the sorted elements. Program: / *********************************************************** Program to perform recursive simple selection sort * * function * ********************************************************** / / / Include required header files #include / / For cout and cin Statement #include using namespace std; / *selection sort function to declare the array element and index element * / void RecursiveselectSort(int arr[], int sindex, int len); / / main function int main() { / / declare the list, sval, temval, m variables int lval[10], sval, temval, m, j; / / using for loop to increment the random numbers for (int m = 0;m 10;m++) { / / rand()stored in the length value variables lval[m] = rand() % 10 + 1; } / / display the before sorted elements cout "before sorted elements are\n"; / / using for loop to increment the length value for (int m = 0;m 10;m++) { / / print the length of the element cout lval[m] " "; / * using if loop to check the condition of the random number * / if ((m + 1) % 20 == 0) cout endl; } / / recursive selection sort function RecursiveselectSort(lval, 0, 10); / / display the sorted random number elements cout "\nAfter Sorted elements are\n"; / / using for loop to increment the length value for (int m = 0;m 10;m++) { / / print the length of the element cout lval[m] " "; / * using if loop to check the condition of the random number * / if ((m + 1) % 20 == 0) cout endl; } / / return zero value return 0; } / * recursive selection sort function to declare the variables array, start index and length variables * / void RecursiveselectSort(int arr[], int sindex, int len) { / * using if condition check start index greater than are equal to length-1 * / if (sindex = len - 1) / / return the start index value return; / * declare the minimum index position and stored to the variables start index values * / int minIndex = sindex; / * using for loop to check the position of the start index value * / for (int index1 = sindex + 1; index1 len; index1++) { / * check the condition index value is less then the start index value * / if (arr[index1] arr[minIndex]) / / store the minimum index value minIndex = index1; } / / swap the index value into the list elements int temval = arr[sindex]; arr[sindex] = arr[minIndex]; arr[minIndex] = temval; / / calling recursively selection sort algorithm RecursiveselectSort(arr, sindex + 1, len); } Sample Output: Before sorted elements are 4 7 8 6 4 6 7 3 10 2 After Sorted elements are 2 3 4 4 6 6 7 7 8 10