Skip to content

Instantly share code, notes, and snippets.

@vicegd
Created February 23, 2020 07:56
Show Gist options
  • Save vicegd/d4f43207668a822e8e707ddfa234c03a to your computer and use it in GitHub Desktop.
Save vicegd/d4f43207668a822e8e707ddfa234c03a to your computer and use it in GitHub Desktop.
Sorting algorithm: Direct insertion method
public void sort(int[] elements) {
int j;
int pivot;
for (int i = 1; i < elements.length; i++) {
pivot = elements[i];
j = i-1;
while (j >= 0 && pivot < elements[j]) {
elements[j+1] = elements[j];
j--;
}
elements[j+1] = pivot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment