This project will allow you to compare & contrast different 4 sorting techniques, the last of which will be up to you to select. You will implement the following: Bubble Sort (pair-wise) Bubble Sort (list-wise) [This is the selection sort] Merge Sort Your choice (candidates are the heap, quick, shell, cocktail, bucket, or radix sorts) [These will require independent research)
I need help with doing this in java.
This project will allow you to compare & contrast different 4 sorting techniques, the last of which will be up to you to select. You will implement the following:
- Bubble Sort (pair-wise)
- Bubble Sort (list-wise) [This is the selection sort]
- Merge Sort
- Your choice (candidates are the heap, quick, shell, cocktail, bucket, or radix sorts) [These will require independent research)
This project will allow you to compare & contrast different 4 sorting techniques, the last of which will be up to you to select. You will implement the following:
- Bubble Sort (pair-wise)
- Bubble Sort (list-wise) [This is the selection sort]
- Merge Sort
- Your choice (candidates are the heap, quick, shell, cocktail, bucket, or radix sorts) [These will require independent research)
CompSorting .java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CompSorting {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc=new Scanner(new File("inp.txt"));
String[] names=new String[600];
String[] names1=new String[600];
String[] names3=new String[600];
int i=0;
while(sc.hasNextLine())
{
String s=sc.next();
names[i]=s;
names1[i]=s;
names3[i]=s;
i++;
}
String[] names2=new String[i];
for(int di=0;di<i;di++)
names2[di]=names[di];
sc.close();
sort_bubbleSort(names,i);
System.out.println("After bubble sort ");
for(int k=0;k<i;k++)
System.out.print(names[k]+" ");
System.out.println();
System.out.println();
sort_selectionSort(names1,i);
System.out.println("After selection sort ");
for(int k=0;k<i;k++)
System.out.print(names[k]+" ");
System.out.println();
System.out.println();
System.out.println("After merge sort ");
for(int k=0;k<i;k++)
System.out.print(names[k]+" ");
}
// Function to perform bubble sort
public static void sort_bubbleSort(String[] names,int cou)
{
for(int k=0;k<cou;k++)
// For each pass
for (int di = 0; di < cou-1; di++)
for (int dj = 0; dj < cou-di-1; dj++)
// Swap if the names care out of order
if (names[dj+1].compareTo(names[dj])<0)
{
String tem = names[dj];
names[dj] = names[dj+1];
names[dj+1] = tem;
}
}
public static void sort_selectionSort(String[] names1,int cou)
{
for (int di = 0; di < cou- 1; ++di)
{
int index_min = di;
for (int dj = di + 1; dj < cou; ++dj)
{
if (names1[dj].compareTo(names1[index_min]) < 0)
{
index_min = dj;
}
}
String tem = names1[di];
names1[di] = names1[index_min];
names1[index_min] = tem;
}
}
public static void sort_mergeSort(String[] names2) {
if (names2.length >= 2) {
String[] lef = new String[names2.length / 2];
String[] ri = new String[names2.length - names2.length/ 2];
for (int di = 0; di < lef.length; di++) {
lef[di] = names2[di];
}
for (int di = 0; di < ri.length; di++) {
ri[di] = names2[di + names2.length / 2];
}
sort_mergeSort(lef);
sort_mergeSort(ri);
sort_merge(names2, lef, ri);
}
}
public static void sort_merge(String[] names2, String[] lef, String[] ri) {
int da = 0;
int db = 0;
for (int di = 0; di < names2.length; di++) {
if (db >= ri.length || (da < lef.length && lef[da].compareToIgnoreCase(ri[db]) < 0)) {
names2[di] = lef[da];
da++;
} else {
names2[di] = ri[db];
db++;
}
}
}
}
inp.txt
abc
bca
cba
qwe
ewq
wqe
tyu
oiu
jkh
fds
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images