import java.io.*;
import java.util.Arrays;
public class tester {
static public class LinkedList {
Node head; // head of list
Node sorted;
int size;
// Linked list Node.
// This inner class is made static
// so that main() can access it
public class Node {
Object data;
Node next;
Node() {
}
// constructor
Node(Object data) {
this.data = data;
next = null;
}
// constructor
Node(Object data, Node n) {
this.data = data;
next = n;
}
}
public void addFirst(Object d) {
head = new Node(d);
size++;
}
public void addLast(Object d) {
Node temp = head;
while (temp != null) {
temp = temp.next;
}
temp = new Node(d,null);
size++;
}
int getSize() {
return size;
}
boolean isSorted()
{
if (head == null)
return true;
// Traverse the list till last node and return
// false if a node is smaller than or equal
// its next.
for (Node t = head; t.next != null; t=t.next)
if ((int)t.data <= (int)t.next.data)
return false;
return true;
}
void InsertionSort()
{
// Initialize sorted linked list
sorted = null;
Node current = head;
// Traverse the given linked list and insert every
// node to sorted
while (current != null)
{
// Store next for next iteration
Node next = current.next;
// insert current in sorted linked list
sortedInsert(current);
// Update current
current = next;
}
// Update head_ref to point to sorted linked list
head = sorted;
}
void sortedInsert(Node newnode)
{
/* Special case for the head end */
if (sorted == null || (int)sorted.data >= (int)newnode.data)
{
newnode.next = sorted;
sorted = newnode;
}
else
{
Node current = sorted;
/* Locate the node before the point of insertion */
while (current.next != null && (int)current.next.data < (int)newnode.data)
{
current = current.next;
}
newnode.next = current.next;
current.next = newnode;
}
}
public void MergeSort() {
Queue q = new Queue();
int count = 0;
int[] sublist1 = null;
int[] sublist2 = null;
int[] tempList = null;
// Node cur = head;
for (Node cur = head; cur != null; cur = cur.next) {
LinkedList newList = new LinkedList();
newList.addFirst(cur.data);
q.enqueue(newList);
}
while (q.size > 1) {
sublist1[count] = (int) q.dequeue();
if (q.size >= 1) {
sublist2[count] = (int) q.dequeue();
}
Arrays.sort(sublist1);
Arrays.sort(sublist2);
tempList = merge(sublist1, sublist2);
}
q.enqueue(tempList);
}
public int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
answer[k++] = a[i] < b[j] ? a[i++] : b[j++];
while (i < a.length)
answer[k++] = a[i++];
while (j < b.length)
answer[k++] = b[j++];
return answer;
}
}
static class Queue {
int front, rear, size;
int capacity;
Object array[];
// constructor
public Queue() {
front = 0;
rear = 0;
size = 0;
}
// constructor
public Queue(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new Object[this.capacity];
}
boolean isFull(Queue queue) {
return (queue.size == queue.capacity);
}
boolean isEmpty(Queue queue) {
return (queue.size == 0);
}
void enqueue(Object newList) {
if (isFull(this))
return;
this.rear = (this.rear + 1) % this.capacity;
this.array[this.rear] = newList;
this.size = this.size + 1;
}
Object dequeue() {
if (isEmpty(this))
return Integer.MIN_VALUE;
Object item = (int) this.array[this.front];
this.front = (this.front + 1) % this.capacity;
this.size = this.size - 1;
return item;
}
}
public static void main(String[] args) {
LinkedList A = new LinkedList();
LinkedList A2 = new LinkedList();
int ramdomListSize = 20000;
for(int i = 0; i < ramdomListSize; i++) {
int randomInt = (int)(Math.random() * 3000000);
A.addLast(randomInt);
A2.addLast(randomInt);
}
//measure the time cost of merge sort
double then = System.currentTimeMillis();
A.MergeSort();
double now = System.currentTimeMillis();
System.out.println("Time cost in milliseconds for mergesort " + (now - then));
System.out.println(A.isSorted()); //verify that your merge sort implementation works.
System.out.println("Size of list A is: " + A.getSize());
//measure the time cost of insertion sort
then = System.currentTimeMillis();
A2.InsertionSort();
now = System.currentTimeMillis();
System.out.println("Time cost in milliseconds for insertionsort " + (now - then));
System.out.println(A2.isSorted() ); //verify that your insertion sort works.
System.out.println("Size of list A2 is: " + A2.getSize());
}//end of main
}
Expected output:
Time cost in milliseconds for mergesort: 37.0
true
Size of list A is: 20000
Time cost in milliseconds for insertionsort: 660.0
true
Size of list A2 is: 20000
My Output:
Time cost in milliseconds for mergesort: 1.0
true
Size of list A is: 20000
Time cost in milliseconds for insertionsort: 0.0
true
Size of list A2 is: 20000
Your InsertionSort()
doesn't do anything.
Node current = head;
while (current != null) {
....
}
You never assign head
field, so it is always null and the loop is never executed.
The only assignment to head
is done in addFirst()
method which is never called.
You should declare a field Node current
to keep track of the last Node object. Your method addLast(Object d)
does not add the object to the list. Storing a different object in a variable does not affect the previously stored object so temp = new Node(d,null);
will only change value of the variable temp
. The method should be like this
public void addLast(Object d) {
if (current != null) {
current.next = new Node(d,null);
current = current.next;
}
else {
head = new Node(d, null);
current = head;
}
size++;
}
Your implementation of merge sort is also incorrect. addFirst
should store previous head
before updating and make the next
field of the new head point to the old one. sublist1
and sublist2
are always null
and count
always remains 0. If you want to sort using merge sort, Arrays.sort
should not be used because it uses quick sort for primitive types like int
. Check out the correct implementation of merge sort here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.