Pygments for Jekyll Sample - Vim

- 2015-03-24 -

Merge sort in various languages using the Vim theme

Theme

.highlight {
  white-space: pre;
  overflow: auto;
  word-wrap: normal; /* horizontal scrolling */
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  padding: 15px;
  padding-left: 10px;
  background: #999999;
  color: #C1C2C3;
}


.highlight .hll { background-color: #222222 }
.highlight .c { color: #000080 } /* Comment */
.highlight .err { color: #cccccc; border: 1px solid #FF0000 } /* Error */
.highlight .g { color: #cccccc } /* Generic */
.highlight .k { color: #cdcd00 } /* Keyword */
.highlight .l { color: #cccccc } /* Literal */
.highlight .n { color: #cccccc } /* Name */
.highlight .o { color: #3399cc } /* Operator */
.highlight .x { color: #cccccc } /* Other */
.highlight .p { color: #cccccc } /* Punctuation */
.highlight .cm { color: #000080 } /* Comment.Multiline */
.highlight .cp { color: #000080 } /* Comment.Preproc */
.highlight .c1 { color: #000080 } /* Comment.Single */
.highlight .cs { color: #cd0000; font-weight: bold } /* Comment.Special */
.highlight .gd { color: #cd0000 } /* Generic.Deleted */
.highlight .ge { color: #cccccc; font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00cd00 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { color: #cccccc; font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #cdcd00 } /* Keyword.Constant */
.highlight .kd { color: #00cd00 } /* Keyword.Declaration */
.highlight .kn { color: #cd00cd } /* Keyword.Namespace */
.highlight .kp { color: #cdcd00 } /* Keyword.Pseudo */
.highlight .kr { color: #cdcd00 } /* Keyword.Reserved */
.highlight .kt { color: #00cd00 } /* Keyword.Type */
.highlight .ld { color: #cccccc } /* Literal.Date */
.highlight .m { color: #cd00cd } /* Literal.Number */
.highlight .s { color: #cd0000 } /* Literal.String */
.highlight .na { color: #cccccc } /* Name.Attribute */
.highlight .nb { color: #cd00cd } /* Name.Builtin */
.highlight .nc { color: #00cdcd } /* Name.Class */
.highlight .no { color: #cccccc } /* Name.Constant */
.highlight .nd { color: #cccccc } /* Name.Decorator */
.highlight .ni { color: #cccccc } /* Name.Entity */
.highlight .ne { color: #666699; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #cccccc } /* Name.Function */
.highlight .nl { color: #cccccc } /* Name.Label */
.highlight .nn { color: #cccccc } /* Name.Namespace */
.highlight .nx { color: #cccccc } /* Name.Other */
.highlight .py { color: #cccccc } /* Name.Property */
.highlight .nt { color: #cccccc } /* Name.Tag */
.highlight .nv { color: #00cdcd } /* Name.Variable */
.highlight .ow { color: #cdcd00 } /* Operator.Word */
.highlight .w { color: #cccccc } /* Text.Whitespace */
.highlight .mf { color: #cd00cd } /* Literal.Number.Float */
.highlight .mh { color: #cd00cd } /* Literal.Number.Hex */
.highlight .mi { color: #cd00cd } /* Literal.Number.Integer */
.highlight .mo { color: #cd00cd } /* Literal.Number.Oct */
.highlight .sb { color: #cd0000 } /* Literal.String.Backtick */
.highlight .sc { color: #cd0000 } /* Literal.String.Char */
.highlight .sd { color: #cd0000 } /* Literal.String.Doc */
.highlight .s2 { color: #cd0000 } /* Literal.String.Double */
.highlight .se { color: #cd0000 } /* Literal.String.Escape */
.highlight .sh { color: #cd0000 } /* Literal.String.Heredoc */
.highlight .si { color: #cd0000 } /* Literal.String.Interpol */
.highlight .sx { color: #cd0000 } /* Literal.String.Other */
.highlight .sr { color: #cd0000 } /* Literal.String.Regex */
.highlight .s1 { color: #cd0000 } /* Literal.String.Single */
.highlight .ss { color: #cd0000 } /* Literal.String.Symbol */
.highlight .bp { color: #cd00cd } /* Name.Builtin.Pseudo */
.highlight .vc { color: #00cdcd } /* Name.Variable.Class */
.highlight .vg { color: #00cdcd } /* Name.Variable.Global */
.highlight .vi { color: #00cdcd } /* Name.Variable.Instance */
.highlight .il { color: #cd00cd } /* Literal.Number.Integer.Long */

Samples

Javascript
function merge_sort(arr) {  
    var l = arr.length, m = Math.floor(l/2);  
    if (l <= 1) return arr;  
    return merge(merge_sort(arr.slice(0, m)), merge_sort(arr.slice(m)));  
}  
  
function merge(left,right) {  
    var result = [];  
    var ll = left.length, rl = right.length;  
    while (ll > 0 && rl > 0) {  
        if (left[0] <= right[0]) {  
            result.push(left.shift());  
            ll--;  
        } else {  
            result.push(right.shift());  
            rl--;  
        }  
    }  
    if (ll > 0) {  
        result.push.apply(result, left);  
    } else if (rl > 0) {  
        result.push.apply(result, right);  
    }  
    return result;  
}
Python
def mergesort(arr):  
    if len(arr) == 1:  
        return arr  
      
    m = len(arr) / 2  
    l = mergesort(arr[:m])  
    r = mergesort(arr[m:])  
  
    if not len(l) or not len(r):  
        return l or r  
          
    result = []  
    i = j = 0  
    while (len(result) < len(r)+len(l)):          
        if l[i] < r[j]:  
            result.append(l[i])  
            i += 1  
        else:  
            result.append(r[j])  
            j += 1              
        if i == len(l) or j == len(r):              
            result.extend(l[i:] or r[j:])  
            break  
          
    return result  
Ruby
def mergesort(list)  
  return list if list.size <= 1  
  mid = list.size / 2  
  left  = list[0, mid]  
  right = list[mid, list.size]  
  merge(mergesort(left), mergesort(right))  
end  
  
def merge(left, right)  
  sorted = []  
  until left.empty? or right.empty?  
    if left.first <= right.first  
      sorted << left.shift  
    else  
      sorted << right.shift  
    end  
  end  
  sorted.concat(left).concat(right)  
end  
Java
view plaincopy to clipboardprint?
public IList MergeSort(IList list)  
{  
    if (list.Count <= 1)  
        return list;  
  
    int mid = list.Count / 2;  
  
    IList left = new ArrayList();  
    IList right = new ArrayList();  
  
    for (int i = 0; i < mid; i++)  
        left.Add(list[i]);  
  
    for (int i = mid; i < list.Count; i++)  
        right.Add(list[i]);  
  
    return Merge(MergeSort(left), MergeSort(right));  
}  
  
public IList Merge(IList left, IList right)   
{  
    IList rv = new ArrayList();  
      
    while (left.Count > 0 && right.Count > 0)  
        if (((IComparable)left[0]).CompareTo(right[0]) > 0)  
        {  
            rv.Add(right[0]);  
            right.RemoveAt(0);  
        }  
        else  
        {  
            rv.Add(left[0]);  
            left.RemoveAt(0);  
        }  
  
    for (int i = 0; i < left.Count; i++)  
        rv.Add(left[i]);  
  
    for (int i = 0; i < right.Count; i++)  
        rv.Add(right[i]);  
  
    return rv;  
}
Erlang
merge_sort(List) when length(List) =< 1 -> List;
merge_sort(List) ->
    {Left, Right} = lists:split(length(List) div 2, List),
    lists:merge(merge_sort(Left), merge_sort(Right)).
Objective C
-(NSArray *)mergeSort:(NSArray *)unsortedArray
{
    if ([unsortedArray count] < 2)
    {
        return unsortedArray;
    }
    long middle = ([unsortedArray count]/2);   
    NSRange left = NSMakeRange(0, middle);
    NSRange right = NSMakeRange(middle, ([unsortedArray count] - middle));
    NSArray *rightArr = [unsortedArray subarrayWithRange:right];
    NSArray *leftArr = [unsortedArray subarrayWithRange:left];
    //Or iterate through the unsortedArray and create your left and right array
    //for left array iteration starts at index =0 and stops at middle, for right array iteration starts at midde and end at the end of the unsorted array
    NSArray *resultArray =[self merge:[self mergeSort:leftArr] andRight:[self mergeSort:rightArr]];
    return resultArray;
}

-(NSArray *)merge:(NSArray *)leftArr andRight:(NSArray *)rightArr
{
    NSMutableArray *result = [[NSMutableArray alloc] init];
    int right = 0;
    int left = 0;
    while (left < [leftArr count] && right < [rightArr count])
    {
        if ([[leftArr objectAtIndex:left] intValue] < [[rightArr objectAtIndex:right] intValue])
        {
            [result addObject:[leftArr objectAtIndex:left++]];
        }
        else
        {
            [result addObject:[rightArr objectAtIndex:right++]];
        }
    }
    NSRange leftRange = NSMakeRange(left, ([leftArr count] - left));
    NSRange rightRange = NSMakeRange(right, ([rightArr count] - right));
    NSArray *newRight = [rightArr subarrayWithRange:rightRange];
    NSArray *newLeft = [leftArr subarrayWithRange:leftRange];
    newLeft = [result arrayByAddingObjectsFromArray:newLeft];
    return [newLeft arrayByAddingObjectsFromArray:newRight];
}
Swift
import Foundation
 
// Build 100 random numbers between 0 and 100
var numbers = Int[]()
for i in 1..100 {
    let n = Int(arc4random() % 101)
    numbers.append(n)
}
 
func elementsInRange<T>(a: T[], start: Int, end: Int) -> (T[]) {
    var result = T[]()
    
    for x in start..end {
        result.append(a[x])
    }
    
    return result
}
 
func merge<T: Comparable>(a: T[], b: T[], mergeInto acc: T[]) -> T[] {
    if a == [] {
        return acc + b
    } else if b == [] {
        return acc + a
    }
    
    if a[0] < b[0] {
        return merge(elementsInRange(a, 1, a.count), b, mergeInto: acc + [a[0]])
    } else {
        return merge(a, elementsInRange(b, 1, b.count), mergeInto: acc + [b[0]])
    }
}
 
func mergesort<T: Comparable>(a: T[]) -> T[] {
    if a.count <= 1 {
        return a
    } else {
        let firstHalf = elementsInRange(a, 0, a.count/2)
        let secondHalf = elementsInRange(a, a.count/2, a.count)
        
        return merge(mergesort(firstHalf), mergesort(secondHalf), mergeInto: [])
    }
}
 
let sorted = mergesort(numbers)
 
println(sorted)
C#
view plaincopy to clipboardprint?
public IList MergeSort(IList list)  
{  
    if (list.Count <= 1)  
        return list;  
  
    int mid = list.Count / 2;  
  
    IList left = new ArrayList();  
    IList right = new ArrayList();  
  
    for (int i = 0; i < mid; i++)  
        left.Add(list[i]);  
  
    for (int i = mid; i < list.Count; i++)  
        right.Add(list[i]);  
  
    return Merge(MergeSort(left), MergeSort(right));  
}  
  
public IList Merge(IList left, IList right)   
{  
    IList rv = new ArrayList();  
      
    while (left.Count > 0 && right.Count > 0)  
        if (((IComparable)left[0]).CompareTo(right[0]) > 0)  
        {  
            rv.Add(right[0]);  
            right.RemoveAt(0);  
        }  
        else  
        {  
            rv.Add(left[0]);  
            left.RemoveAt(0);  
        }  
  
    for (int i = 0; i < left.Count; i++)  
        rv.Add(left[i]);  
  
    for (int i = 0; i < right.Count; i++)  
        rv.Add(right[i]);  
  
    return rv;  
}
C++
vector<int> merge_sort(vector<int>& vec)  
{  
    // Termination condition: List is completely sorted if it  
    // only contains a single element.  
    if(vec.size() == 1)  
    {  
        return vec;  
    }  
  
    // Determine the location of the middle element in the vector  
    std::vector<int>::iterator middle = vec.begin() + (vec.size() / 2);  
  
    vector<int> left(vec.begin(), middle);  
    vector<int> right(middle, vec.end());  
  
    // Perform a merge sort on the two smaller vectors  
    left = merge_sort(left);  
    right = merge_sort(right);  
  
    return merge(left, right);  
}