Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to read the number and elements of an array entered by a user. S

ID: 3665558 • Letter: W

Question

Write a program to read the number and elements of an array entered by a user. Sort the elements using

1. Bubble Sort Function

2. Shell Sort Function

3. Selection Sort Function

4. Insertion Sort Function

Your program MUST have 6 functions:

a. Main function

b. Introduction function to indicate the problem statement and solutions.

c. Read function to read number of elements and the elements themselves

d. Print function to print the original array, and the sorted arrays after each sort function.

e. Four sort functions. One for each sort routine. You may use following functions in your program.

//Selection Sort Function void SelectionSort(int a[], int n ) { int i, j, first, temp; int n; for (i= n - 1; i > 0; i--) { first = 0; // initialize to subscript of first element for (j=1; j<=i; j++) // locate smallest between positions 1 and i. { if (a[j] < a[first]) first = j; } temp = a[first]; // Swap smallest found with element in position i. a[first] = a[i]; a[i] = temp; } return; //arrays are passed to functions by address; nothing is returned }

// Insertion Sort Function void InsertionSort (int a[], int n ) { int i, j, key; for(j = 1; j < n; j++) // Start with 1 (not 0) { key = a[j]; for(i = j - 1; (i >= 0) && (a[i] < key); i--) // Smaller values move up { a[i+1] = a[i]; } a[i+1] = key; //Put key into its proper location } return; }

// Bubble Sort Function void BubbleSort(int a[], int n) { int i, j, flag = 1; // set flag to 1 to start first pass int temp; // holding variable for(i = 1; (i <= n && flag; i++) { flag = 0; for (j=0; j < (n -1); j++) { if (a[j+1] > a[j]) // ascending order simply changes to < { temp = a[j]; // swap elements a[j] = a[j+1]; a[j+1] = temp; flag = 1; // indicates that a swap occurred. } } } return; //arrays are passed to functions by address; nothing is returned }

//Shell Sort Function void ShellSort ( int a[], int n ) { int i, temp, flag = 1; int d = n; while( flag || (d > 1)) // boolean flag (true when not equal to 0) { flag = 0; // reset flag to 0 to check for future swaps d = (d+1) / 2; for (i = 0; i < (n - d); i++) { if (a[i + d] > a[i]) { temp = a[i + d]; // swap positions i+d and i a[i + d] = a[i]; a[i] = temp; flag = 1; // tells swap has occurred } } } return; }

Explanation / Answer

Due to time constraint iam just writing the main program and functions.Please embed them. Feel free to contact for further queries.

#include<stdio.h>

#include<conio.h>

void bubble_sort(long [], long);

void ShellSort(int *array, int number_of_elements);

void insertion(int [], int );

void selectionSort(int arr[], int n);

{
long array[100], n, c, d, swap;
int ch;

printf("Enter number of elements ");
scanf("%ld", &n);

printf("Enter %ld longegers ", n);

for (c = 0; c < n; c++)
scanf("%ld", &array[c]);

printf("Enter 1 for Bubble Sort, 2 for Shell Sort, 3 for Insertion Sort, 4 for Selection Sort ");
scanf("%d",&ch);

switch(ch);
{
Case 1:
bubble_sort(array, n)
break;

Case 2:
void ShellSort(array, n)
break;
Case 3:

void insertion(array, n)
break;
Case 4:

void selectionSort(array, n)
break;
default:
printf("input correct values");

}

printf("Sorted list in ascending order: ");

for ( c = 0 ; c < n ; c++ )
printf("%ld ", array[c]);

return 0;
}

void bubble_sort(long list[], long n)
{
long c, d, t;

for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
/* Swapping */

t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}

void ShellSort(int *array, int number_of_elements)
{
int iter, jter, increment, temp;
for(increment = number_of_elements/2;increment > 0; increment /= 2)
{
for(i = increment; i<number_of_elements; i++)
{
temp = array[i];
for(j = i; j >= increment ;j-=increment)
{
if(temp < array[j-increment])
{
array[j] = array[j-increment];
}
else
{
break;
}
}
array[j] = temp;
}
}
}

void insertion(int arr[], int size)
{
int i,j,tmp;
for(i=0; i<size; i++)
{
for(j=i-1; j>=0; j--)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
else
break;
}
}
}
#include <stdio.h>

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf(" ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote