cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>
  Insertion sort
  <span>SASS implementation by Jack Dempsey</span>
</h1>
<div class="error"></div>
<ul>
  <li class="list-original">List to sort</li>
  <li class="list-sorted">Sorted list</li>
  <li class="list-unable">Unable to sort</li>
</ul>
            
          
!
            
              /* LIST TO SORT */
$list: (100, 7, 3, a, 0, 10, b, x, 9, 1, 2, 8, 5, 4, 3, 8, 1, 1, 9, m, t);








/* --------------------------- *\
   DON'T GO BEYOND THIS LINE
\* --------------------------- */

/* 
  Insert element at point X
  - $insertList: list where to inset the value
  - $insertIndex: where to insert the value
  - $insertValue: the value
  - $insertLocation: deal with the value that is currently at $insertIndex (before or after)
*/
@function insert($insertList, $insertIndex, $insertValue, $insertLocation: 'after') {
  $result: ();
  @for $i from 1 through length($insertList) {
    @if $i == $insertIndex and $insertLocation == 'before' {
      $result: append($result, $insertValue);
    }
    
    $result: append($result, nth($insertList, $i));

    @if $i == $insertIndex and $insertLocation == 'after' {
      $result: append($result, $insertValue);
    }
  }
  @return $result;
}

$listLength: length($list);
$orderedList: nth($list, 1);
$NaNList: ();

@for $i from 2 through $listLength {

  // this flag is ensuring that the value is not inserted multiple times  
  $flag: true;
  
  // current value to be considered  
  $currentValue: nth($list, $i);
  
  @if type-of($currentValue) == 'number' {
   @for $j from length($orderedList) through 1 {
      $currentOrderedValue: nth($orderedList, $j);

      @if $flag {
        @if $currentOrderedValue <= $currentValue {
          $flag: false;
          $orderedList: insert($orderedList, $j, $currentValue, 'after');
        } @elseif $j > 1 {
          @if $currentOrderedValue > $currentValue and $currentValue > nth($orderedList, $j - 1) {
            $flag: false;
            $orderedList: insert($orderedList, $j - 1, $currentValue);
          }
        } @else {
          $orderedList: insert($orderedList, 1, $currentValue, 'before');
        }
      }
    }   
  } @else {
    $NaNList: append($NaNList, nth($list,$i));
  }
  
}

.list {
  &-original {
    &:after {
      content: '#{$list}';
    }
  }
  
  &-sorted {
    &:after {
      content: '#{$orderedList}';
    }
  }
  
  &-unable {
    @if length($NaNList) > 0 {
      &:after {
        content: '#{$NaNList}';
      }
    } @else {
      display: none;
    }
  }
}


/* STYLING */
body {
  line-height: 1.6em;
  text-align: center;

}

body,
body * {
  box-sizing: border-box;
}

h1 {
  background: #E64A19;
  padding: 1rem;
  color: rgb(255, 255, 255);
  font-weight: 300;
  margin: 0;
  border-bottom: 5px solid #FF9800;
  
  span {
    display: block;
    font-size: .5em;
    padding: .3rem 0 0;
    color: #FFCCBC;
    
  }
}

.error {
  display: none;
  background: #F44336;
  padding: 1rem;
  color: rgb(255, 255, 255);
  
  &:before {
    content: 'Error: ';
    font-weight: 600;
  }
}

ul {
  list-style: none;
  margin: 0;
}

li {
  padding: 1rem;
  
  &:after {
    font-weight: 600;
    display: block;
    font-size: 2rem;
    line-height: 1.3em;
  }
}

/* © Razvan */
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console