GENERAL ENGINEERING Perform the following data structure on Sorting: BASIC PROGRAMMING CREATE an array with at least 5 in size and put elements in the array. Perform the following: Sorting Data Structure operation: 1. Bubble Sort 2. Insertion Sort 3. Selection Sort CHOOSE OPERATION : // if option 1 is selected, sort the elements of the array using Bubble sort: //if option 2 is selected, sort the elements of the array using Insertion sort: //if option 3 is selected, sort the elements of the array using Selection sort: BubbleSort.java import java.util.*; class BubbleSort{ 1 InsertionSort.java // Driver method to test above public static void main(String args[]) { I/declare an array of integers int intarray[] {23,43,13,65,11,62,76,83,9,71,84,34,96,80}; I/print original array System.out.println("Original array: " + Arrays.toString(intArray)); int n = intArray.length; //iterate over the array comparing adjacent elements for (int i = e; i < n-1; i++) for (int j = 0; j =0 && temp <= numArray[j]) 4 3 6 4 5 8. 6 9 10 { 11 9 12 10 //if elements not in order, swap them if (intArray[j] > intArray[j+1]) { int temp - intArray[j]; intArray[j] - intArray[j+1]; intArray[j+1] - temp; 13 11 { 14 12 numArray[j+1] = numArray[j]; 15 13 j- j-1; 16 14 17 15 numArray[j+1] - temp; 18 } 16 //print the sorted array System.out.println("Sorted array: } 19 I/print the sorted array System.out.println("Sorted Array:" + Arrays.tostring(numArray)): } } 17 20 + Arrays.toString(intArray)); 18 21 19 22 20 SelectionSort.java 1 import java.util.*; class SelectionSort { static void sel_sort(int numArray[]) 3 4 5 6 int n = numArray.length; // traverse unsorted array for (int i = e; i < n-1; i++) { // Find the minimum element in unsorted array 8 9 10 11 int min_idx i; for (int j = i+1; j < n; j++) if (numArray[j] < numArray[min_idx]) min_idx = j; 12 13 14 15 16 // swap minimum element with compared element int temp = numArray[min_idx]; numArray[min_idx] = numArray[i]; numArray[i] = temp; 17 18 19 20 21 22 } 23 public static void main(String args[]) { //declare and print the original array 24 25 26 int numArray[] {8,4,1,15,52,12,32,11,23}; System.out.println("Original Array:" + Arrays.tostring(numArray)) 27 28 29 //call selection sort routine sel_sort(numArray); I/print the sorted array System.out.printin("Sorted Array:" + Arrays.tostring(numArray)); 30 31 32 33 34 }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Programming

GENERAL ENGINEERING
Perform the following data structure on Sorting:
BASIC PROGRAMMING
CREATE an array with at least 5 in size and put elements in the array. Perform the
following:
Sorting Data Structure operation:
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
CHOOSE OPE RATION:
// if option 1 is selected, sort the elements of the array using Bubble sort:
//if option 2 is selected, sort the elements of the array using Insertion sort:
//if option 3 is selected, sort the elements of the array using Selection sort:
BubbleSort.java
import java.util.*;
class BubbleSort{
1
2
InsertionSort.java
// Driver method to test above
public static void main(String args[]) {
//declare an array of integers
int intArray[] = {23,43,13,65,11,62,76,83,9,71,84,34,96,8e};
//print original array
System.out.println("Original array:
int n - intArray.length;
3
import java.util.*;
public class InsertionSort {
public static void main(String[] args) {
//declare an array and print the original contents
int[] numArray = {10,6,15,4,1,45};
System.out.println("Original Array:" + Arrays.toString(numArray)
1
4
2
5
6
7
5
+ Arrays.toString(intArray));
6
9
//apply insertion sort algorithm on the array
for(int k=1; k<numArray.length-1; k++)
int temp = numArray[k];
int j- k-1;
7
//iterate over the array comparing adjacent elements
for (int i = 0; i < n-1; i++)
for (int j = e; j <n-i-1; j++)
10
8
{
11
9
12
10
//if elements not in order, swap them
if (intArray[j] > intArray[j+1]) {
int temp - intArray[j];
intArray[j] = intArray[j+1];
intarray[j+1] - temp;
13
while(j>=0 && temp <= numArray[j])
numArray[j+1] - numArray[j];
j = j-1;
11
{
14
12
15
13
16
14
numArray[j+1] = temp;
}
//print the sorted array
System.out.println("Sorted Aray:" + Arrays.tostring(numArray));
17
15
18
16
19
//print the sorted array
17
+ Arrays.toString(intArray));
System.out.println("Sorted aray:
}
}
20
18
21
19
22
20
}
SelectionSort.java
1
import java.util.*;
2.
class SelectionSort
{
static void sel_sort(int numArray[])
{
int n = numArray.length;
4
// traverse unsorted array
for (int i = 0; i < n-1; i++)
8
{
// Find the minimum element in unsorted array
int min_idx - i;
for (int j = i+1; j < n; j++)
if (numArray[j] < numArray[min_idx])
10
11
12
13
14
15
min_idx = j;
16
// swap minimum element with compared element
int temp = numArray[min_idx];
numArray[min_idx] = numArray[i];
numArray[i] = temp;
17
18
19
20
21
22
}
23
24
public static void main(String args[])
25
//declare and print the original array
int numArray[] = {8,4,1,15,52,12,32,11,23};
System.out.println("Original Array:" + Arrays.toString(numArray))
26
27
28
29
//call selection sort routine
sel_sort(numArray);
//print the sorted array
System.out.println("Sorted Array:" + Arrays.tostring(numArray));
}
30
31
32
33
34
}
Transcribed Image Text:GENERAL ENGINEERING Perform the following data structure on Sorting: BASIC PROGRAMMING CREATE an array with at least 5 in size and put elements in the array. Perform the following: Sorting Data Structure operation: 1. Bubble Sort 2. Insertion Sort 3. Selection Sort CHOOSE OPE RATION: // if option 1 is selected, sort the elements of the array using Bubble sort: //if option 2 is selected, sort the elements of the array using Insertion sort: //if option 3 is selected, sort the elements of the array using Selection sort: BubbleSort.java import java.util.*; class BubbleSort{ 1 2 InsertionSort.java // Driver method to test above public static void main(String args[]) { //declare an array of integers int intArray[] = {23,43,13,65,11,62,76,83,9,71,84,34,96,8e}; //print original array System.out.println("Original array: int n - intArray.length; 3 import java.util.*; public class InsertionSort { public static void main(String[] args) { //declare an array and print the original contents int[] numArray = {10,6,15,4,1,45}; System.out.println("Original Array:" + Arrays.toString(numArray) 1 4 2 5 6 7 5 + Arrays.toString(intArray)); 6 9 //apply insertion sort algorithm on the array for(int k=1; k<numArray.length-1; k++) int temp = numArray[k]; int j- k-1; 7 //iterate over the array comparing adjacent elements for (int i = 0; i < n-1; i++) for (int j = e; j <n-i-1; j++) 10 8 { 11 9 12 10 //if elements not in order, swap them if (intArray[j] > intArray[j+1]) { int temp - intArray[j]; intArray[j] = intArray[j+1]; intarray[j+1] - temp; 13 while(j>=0 && temp <= numArray[j]) numArray[j+1] - numArray[j]; j = j-1; 11 { 14 12 15 13 16 14 numArray[j+1] = temp; } //print the sorted array System.out.println("Sorted Aray:" + Arrays.tostring(numArray)); 17 15 18 16 19 //print the sorted array 17 + Arrays.toString(intArray)); System.out.println("Sorted aray: } } 20 18 21 19 22 20 } SelectionSort.java 1 import java.util.*; 2. class SelectionSort { static void sel_sort(int numArray[]) { int n = numArray.length; 4 // traverse unsorted array for (int i = 0; i < n-1; i++) 8 { // Find the minimum element in unsorted array int min_idx - i; for (int j = i+1; j < n; j++) if (numArray[j] < numArray[min_idx]) 10 11 12 13 14 15 min_idx = j; 16 // swap minimum element with compared element int temp = numArray[min_idx]; numArray[min_idx] = numArray[i]; numArray[i] = temp; 17 18 19 20 21 22 } 23 24 public static void main(String args[]) 25 //declare and print the original array int numArray[] = {8,4,1,15,52,12,32,11,23}; System.out.println("Original Array:" + Arrays.toString(numArray)) 26 27 28 29 //call selection sort routine sel_sort(numArray); //print the sorted array System.out.println("Sorted Array:" + Arrays.tostring(numArray)); } 30 31 32 33 34 }
Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY