Write a program to read the number and elements of an array entered by a user. S
ID: 673533 • 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:
Main function
Introduction function to indicate the problem statement and solutions.
Read function to read number of elements and the elements themselves
Print function to print the original array, and the sorted arrays after each sort function.
Four sort functions. One for each sort routine.
Explanation / Answer
Hi
Please refer below programs. Codes are in C as well in Java.
Bubble sort Implementation: C code
#include<stdio.h>
#include<conio.h>
void bubble_sort(int[], int);
void main() {
int arr[30], num, i;
printf(" Enter no of elements :");
scanf("%d", &num);
printf(" Enter array elements :");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
bubble_sort(arr, num);
getch();
}
void bubble_sort(int iarr[], int num) {
int i, j, k, temp;
printf(" Unsorted Data:");
for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}
for (i = 1; i < num; i++) {
for (j = 0; j < num - 1; j++) {
if (iarr[j] > iarr[j + 1]) {
temp = iarr[j];
iarr[j] = iarr[j + 1];
iarr[j + 1] = temp;
}
}
printf(" After pass %d : ", i);
for (k = 0; k < num; k++) {
printf("%5d", iarr[k]);
}
}
}
C program for insertion sort:
#include <stdio.h>
#include <math.h>
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf(" ");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0])
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Java code to shell sorting Algorithm
Shell Sort - C Program Source Code
#include<stdio.h>
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;
}
}
}
int main()
{
int number_of_elements;
scanf("%d",&number_of_elements);
int array[number_of_elements];
int iter;
for(iter = 0;iter < number_of_elements;iter++)
{
scanf("%d",&array[iter]);
}
/* Calling this functions sorts the array */
ShellSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
{
printf("%d ",array[iter]);
}
printf(" ");
return 0;
}
Shell Sort - Java Program Source Code
import java.io.*;
class ShellSort
{
void ShellSort(int array[], int number_of_elements)
{
int iter, jter, increment, temp,i,j;
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;
}
}
}
int main()throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int number_of_elements;
System.out.println("Enter the number of elements");
number_of_elements=Integer.parseInt(in.readLine());
int array[]=new int[number_of_elements];
int iter;
System.out.println("Enter the elements one by one");
for(iter = 0;iter < number_of_elements;iter++)
{
array[iter]=Integer.parseInt(in.readLine());;
}
/* Calling this functions sorts the array */
ShellSort(array,number_of_elements);
for(iter = 0;iter < number_of_elements;iter++)
{
System.out.print(array[iter]+" ");
}
System.out.print(" ");
return 0;
}
}
Selection sort : Java code
package com.java2novice.algos;
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {10,34,2,56,7,67,88,42};
int[] arr2 = doSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
Selection sort: C Code
*c program for sorting array using shell sorting method*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
for(k=0; k<num; k++)
{
printf(" Enter %d number : ",k+1);
scanf("%d",&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf(" **** Shell Sorting **** ");
for(k=0; k<num; k++)
printf("%d ",arr[k]);
getch();
return 0;
}
Disclaimer: Some coding have been refered from http://java2novice.com ,http://cprogrammingcodes.blogspot.in
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.