void ReverseLinkedList(LinkedList ls)
{
if ( ls == null || ls.Head == null )
return;
ListNode head = ls.Head;
ListNode current = head.Next;
while(current!=null)
{
ListNode tmp = current;
current = current.Next;
tmp.Next = head;
head = tmp;
}
ls.Head.Next = null;
ls.Head = head;
}
An innovative, experienced solutions architect with strong focus on public cloud architecture, solution design and leading technical team. Zhen has 15 years experience designing and implementing a wide spectrum of enterprise level solutions. In the last 4 years, he is committed to evangelising Azure, DevOps and Scrum. His recent focus are enterprise digitisation, cloud governance, reference architecture, IoT, Big Data, Microservices, AWS.
Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts
Monday, November 16, 2009
c# revert a LinkedList
c# merge sort algorithm implementation
private int[] MergeSort(int[] a)
{
if ( a.Length == 1)
return a;
int middle = a.Length / 2;
int[] left = new int[middle];
for (int i = 0 ; i < middle ; i ++)
{
left[ i ] = a[ i ];
}
int[] right = new int[a.Length - middle];
for( int i = 0; i < a.Length - middle; i++ )
{
right[i] = a[i+middle];
}
left = MergeSort( left );
right = MergeSort( right );
int leftptr = 0;
int rightptr = 0;
int[] sorted = new int[a.Length];
for(int k = 0 ; k < a.Length; k++)
{
if ( rightptr == right.Length || ((leftptr < left.Length ) && (left[leftptr] <= right[rightptr])))
{
sorted[ k ] = left[ leftptr ];
leftptr++;
}
else if( leftptr == left.Length || ((rightptr < right.Length ) && (right[rightptr] <= left[leftptr] )))
{
sorted[k] = right[rightptr];
rightptr++;
}
}
return sorted;
}
c# Quick Sort Algorithm Implementation
private void QuickSort( int[] a, int left, int right ) { if( a == null ) return; int i = left; int j = right; int pivot = a[( left + right ) / 2]; while( i <= j ) { while( a[i] < pivot ) i++; while( a[j] > pivot ) j--; if( i <= j ) { int tmp = a[i]; a[i++] = a[j]; a[j--] = tmp; } } if( j > left ) { QuickSort( a, left, j ); } if( i < right ) { QuickSort( a, i, right ); } }
c# insert sort algorithm implementation
private void insert_sort( int[] s )
{
for( int k = 1; k < s.Length ; k++ )
{
int value = s[k];
for( int i = 0; i <= k - 1; i++ )
{
if( value < s[i] )
{
for( int j = k - 1; j >= i; j-- )
s[j + 1] = s[j];
s[i] = value;
break;
}
}
}
}
Subscribe to:
Posts (Atom)