Question

In: Computer Science

This is an exercise in correctly implementing insertion sort and selection sort. This assignment includes a...

This is an exercise in correctly implementing insertion sort and selection sort.


This assignment includes a text data file containing information on tutorial websites for a variety of programming languages. The data file is named Tutorials. It contains records of programming tutorial websites. The record structure for this text file is:


FIELD 1 = Programming Language

FIELD 2 = Name and Description of Website

FIELD 3 = URL Web Address of Language Tutorial


The structure of the file is that there is a separate line for each field/property/attribute of the record. Every three lines makes up one physical record.


For those of you who took computer science I here at CCP, you covered using a bubble sort to sort elements in a collection.


In your project, create two methods for the project – one that performs a selection sort on your Tutorial Website object array and one that performs an insertion sort on your Tutorial Website object array.


You should create a user defined object of Tutorial Website type. The object should have three properties:

Language Name
Website Description
Website URL

Your object will need to implement the comparable interface for this object. You may choose the property is used to determine the order of your object. Language name would be the best choice.


Your project should read the list of programming language tutorials in from the text data file, and load the data into a Tutorial Website object . In the process it should add the object into an array. After the array is fully loaded, print the list in unsorted order. sort the list in the array in ascending order using a Selection Sort Algorithm. After the list in the array is sorted in ascending order using the selection sort, print the list to the screen and write the list to CSV file. You should then sort the list in descending order using a Insertion Sort Algorithm. After the list in the array is sorted in ascending order using the selection sort, print the list to the screen and write the list to a CSV file in the project folder. After the list in the array is sorted in descending order using the Insertion sort, print the list to the screen and write the list to a CSV file in the project folder.

Solutions

Expert Solution

programming language is a formal language comprising a set of instructions that produce various kinds of output. Programming languages are used in computer programming to implement algorithms.import java.util.*;//imported util package

A website is a collection of web pages and related content that is identified by a common domain name and published on at least one web server. Notable examples are wikipedia.org, google.com, and amazon.com.

All publicly accessible websites collectively constitute the World Wide Web. There are also private websites that can only be accessed on a private network, such as a company's internal website for its employees.

Websites are typically dedicated to a particular topic or purpose, such as news, education, commerce, entertainment, or social networking. Hyperlinking between web pages guides the navigation of the site, which often starts with a home page.

A Uniform Resource Locator (URL), colloquially termed a web address,is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. A URL is a specific type of Uniform Resource Identifier (URI), although many people use the two terms interchangeably. URLs occur most commonly to reference web pages (http), but are also used for file transfer (ftp), email (mailto), database access (JDBC), and many other applications.

Most web browsers display the URL of a web page above the page in an address bar.

import java.io.*;//imported io package

class TutorialWebsite implements Comparable<TutorialWebsite>{//TutorialWebsite class

String LanguageName;

String WebsiteDescription;

String WebsiteURL;

TutorialWebsite(String name, String description, String url){

LanguageName = name;

WebsiteDescription = description;

WebsiteURL = url;

}

public int compareTo(TutorialWebsite tw)

{

if(LanguageName == tw.LanguageName)

return 0;

else if(LanguageName.compareTo(tw.LanguageName)>0)

return 1;

else return -1;

}

}

class Main{/*main class named Main*/

/* descending write method writes the data into Descending.csv file by taking arrayList as argument*/

void descendingwrite(ArrayList<TutorialWebsite> arr)throws IOException{

DataOutputStream dos= new DataOutputStream(new FileOutputStream("Descending.csv"));

for(TutorialWebsite tutorial:arr)

{

dos.writeChars(tutorial.LanguageName);

dos.writeChars("\n");

dos.writeChars(tutorial.WebsiteDescription);

dos.writeChars("\n");

dos.writeChars(tutorial.WebsiteURL);

dos.writeChars("\n");

}

dos.close();

}

void ascendingwrite(ArrayList<TutorialWebsite> arr)throws IOException

{

DataOutputStream dos= new DataOutputStream(new FileOutputStream("Ascending.csv"));

for(TutorialWebsite tutorial:arr)

{

dos.writeChars(tutorial.LanguageName);

dos.writeChars("\n");

dos.writeChars(tutorial.WebsiteDescription);

dos.writeChars("\n");

dos.writeChars(tutorial.WebsiteURL);

dos.writeChars("\n");

}

dos.close();

}

void selectionSort()throws IOException{/*selectionSort method, sorts the list in ascending order*/

File file = new File("Tutorial.txt");

Scanner read = new Scanner(file);/*created an object of Scanner class named input*/

ArrayList<TutorialWebsite> arr = new ArrayList<TutorialWebsite>();

while(read.hasNext()){

arr.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));

}

int n = arr.size();

// One by one move boundary of unsorted subarray

for (int i = 0; i < n-1; i++)

{

// In unsorted array find the minimum element

int min_idx = i;

for (int j = i+1; j < n; j++)

if (arr.get(j).LanguageName.compareTo(arr.get(min_idx).LanguageName)<0)

min_idx = j;

// Swap the found minimum element with the first

// element

Collections.swap(arr,i,min_idx);

}

ascendingwrite(arr);/*called the method ascendingwrite by passing arr*/

System.out.println("written into Ascending file successfully");/*if the above descendingwrite method works fine then this statement is printed*/

}

void insertionSort()throws IOException{/*insertionSort method, sorts the list in descending order*/

File file = new File("Tutorial.txt");

Scanner read = new Scanner(file);/*created an object of Scanner class named input*/

ArrayList<TutorialWebsite> arr = new ArrayList<TutorialWebsite>();

while(read.hasNext()){

arr.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));

}

int n = arr.size();

for (int i=1; i<n; ++i)

{

String name = arr.get(i).LanguageName;

String description = arr.get(i).WebsiteDescription;

String url = arr.get(i).WebsiteURL;

int 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.get(j).LanguageName.compareTo(name)<0)

{

arr.set(j+1,arr.get(j));

j = j-1;

}

arr.set(j+1,new TutorialWebsite(name, description, url));

}

descendingwrite(arr);/*called the method descendingwrite by passing arr*/

System.out.println("written into Descending file successfully");/*if the above descendingwrite method works fine then this statement is printed*/

}

public static void main(String args[])throws IOException{/*main method, execution of program starts from here*/

File file = new File("Tutorials.txt");/*creates a file object by passing the file name Tutorials.txt*/

Scanner read = new Scanner(file);/*created an object of Scanner class named read*/

ArrayList<TutorialWebsite> tw = new ArrayList<TutorialWebsite>();

while(read.hasNext()){/*reads the data into ArrayList named tw*/

tw.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));

}

Collections.sort(tw);//sorted using comparable interface.

Main main = new Main();/*created an object to Main class to call to selectionSort and insertionSort methods*/

main.insertionSort();/*a call to insertionSort is invoked*/

main.selectionSort();/*a call to selectionSort is invoked*/

}

}


Related Solutions

For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random...
Exercise 4–Timing Sorting AlgorithmCollect the run times for either selection sort or insertion sort (use random values for an array and sorted values; sorted the same list twice and collect time each time) for the following array sizes: 1000, 2000, and 10000. You should be able to confirm that the runtime is n^2 for unsorted list (i.e., going from 1000 to 2000 should be about 4 times slower and going from 1000 to 10000 should be about 100times slower). Question...
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort....
Write in python: Go through the visualization of the Selection Sort, Bubble Sort and Insertion Sort. Write a Pseudo code first for each of these sort methods. After writing the pseudo code write python code from the pseudo code.
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT