java


InsertionSort on LinkedList data structure


I have a List.cs file with this code:
class MyDataList : DataList
{
class MyLinkedListNode
{
public MyLinkedListNode nextNode { get; set; }
public double data { get; set; }
public MyLinkedListNode(double data)
{
this.data = data;
}
}
MyLinkedListNode headNode;
MyLinkedListNode prevNode;
MyLinkedListNode currentNode;
public MyDataList(int n, int seed)
{
length = n;
Random rand = new Random(seed);
headNode = new MyLinkedListNode(rand.NextDouble());
currentNode = headNode;
for (int i = 1; i < length; i++)
{
prevNode = currentNode;
currentNode.nextNode = new MyLinkedListNode(rand.NextDouble());
currentNode = currentNode.nextNode;
}
currentNode.nextNode = null;
}
public override double Head()
{
currentNode = headNode;
prevNode = null;
return currentNode.data;
}
public override double Next()
{
prevNode = currentNode;
currentNode = currentNode.nextNode;
return currentNode.data;
}
public override void Swap(double a, double b)
{
prevNode.data = a;
currentNode.data = b;
}
}
And a Program.cs with this code
class Insertion_Sort
{
static void Main(string[] args)
{
int seed = (int)DateTime.Now.Ticks & 0x0000FFFF;
Test_Array_List(seed);
}
public static void InsertionSort(DataList items)
{
}
public static void Test_Array_List(int seed)
{
int n = 12;
MyDataList mylist = new MyDataList(n, seed);
Console.WriteLine("\n LIST \n");
mylist.Print(n);
InsertionSort(mylist);
mylist.Print(n);
}
}
abstract class DataList
{
protected int length;
public int Length { get { return length; } }
public abstract double Head();
public abstract double Next();
public abstract void Swap(double a, double b);
public void Print(int n)
{
Console.Write(" {0:F5} ", Head());
for (int i = 1; i < n; i++)
Console.Write(" {0:F5} ", Next());
Console.WriteLine();
}
}
Could someone help me with the InsertionSort here? I'm having trouble getting the code to work even though there are plenty of examples online I can't get it to work on my case.
I will try to answer the question to give you tid-bits about how to use insertion sort on linked list. Hopefully you would be able to learn and apply this in your code.
Basically insertion sort is like you sort cards in your hand. You pick one element scan it from right-left until you find a spot where your current card would fit-in. Idea is all cards in your hand are sorted while remaining cards lying are unsorted.
Using the same theory on link list, you start from head, pick up the first node, and create a sorted list. At the end, your input list should be null and sorted list would the list with all elements sorted.
Here is a pseudo code :
struct node* sort_link_list(struct node* plist) {
/* empty list or only one element in a list */
if(plist == null or plist->next == null) {
return plist;
}
/* Head of sorted list, initially sorted list in null */
struct node * head = null;
/* Scan each element of the input list */
while(plist != null) {
/* temp is the current node to be placed in sorted list */
struct node* temp = plist;
/* input list is one short */
plist = plist->next;
/* if sorted list is empty or current node is to be placed in front */
if(head == null or temp->val < head->val ) {
temp->next = head;
head = temp;
}else {
/* scan the sorted list and place temp in the appropriate location */
struct node *p = head;
while(p != null) {
/* position found or you reached end of the sorted list*/
if(p->next == null or temp->val < p->val) {
temp->next = p->next;
p->next = temp;
break;
}
p = p->next;
}
}
}
return head;
}

Related Links

How to access my static ArrayList in Android from another class
Hibernate sql query processing slower then normal query
Elapsed time format HH:mm:ss
What is a difference between traditional loop and for-each loop? [duplicate]
different color for first row in JTable with pagination
Constructor call must be the first statement in a constructor in super()
How do I call an interface method if an object implements that interface?
UpdateString not implemented by SQLite JDBC driver
Monitoring the size of the Netty event loop queues
Kafka producer sending invalid characters
how to get listview data from parseJson
How to update a document in MongoDB using ObjectID in Java
Need help in JavaFx Textfield Focus property about changing the text after focus out
Getting actor position with its padding
How to correctly throw an exception
get java installation path using shell script in rhel

Categories

HOME
date
gremlin
dictionary
google-api-php-client
kalman-filter
sqlite-net-extensions
malloc
graphql
ios-charts
communication
offline
php-7.1
timeout
fallback
windows-server-2012
serverless-framework
opentracing
transformation
google-static-maps
lucene.net
swingx
one-hot-encoding
java-3d
physics-engine
arabic
realex-payments-api
language-agnostic
captiveportal
format-specifiers
jndi
url-scheme
intel-pin
dbclient
google-cloud-nl
librato
semantic-versioning
ioio
catel
column-family
code-contracts
lxd
catalog
hot-module-replacement
espeak
bootstrapper
awt
sas-visual-analytics
janrain
botbuilder
angular2-meteor
menuitem
blogengine.net
winscp-net
togetherjs
nstextview
font-size
dtexec
redundancy
smart-table
or-tools
trash
ubuntu-10.04
bluegiga
pagerank
ios4
separator
sdhc
quicklisp
coveralls
wyam
dukescript
web-controls
clipperlib
kineticjs
system.reflection
angularjs-ng-click
nsbutton
preferences
ceil
has-many-through
rebol3
doskey
jboss-weld
ember-charts
srs
stxxl
intentservice
gnu-smalltalk
inbox
ftps
batterylevel
buster.js
gwt-rpc
tinn-r
browser-detection
dmoz
nsnetservice
labwindows
viewswitcher
objective-c-2.0
celltable
remember-me
digest-authentication
mirah
ncqrs
user-friendly
uimenucontroller
anti-piracy
zend-decorators
windows-live-messenger

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App