Insertion sort algorithm in Java

Hello our followers, today we want to share you our knowledge of sorting algorithms with example for Java language. This type of algorithm can be used in different languages, such as C, C + +, C #. But we will try to tell you how to create it in the famous and well known language Java with examples of work for the platform Android.

Let's start with what we call the sort ordered set with any data assignments for a given query. If you use numbers, sorting can be carried out from small to large and vice versa. If you need to sort words, you can do it in alphabetical order, or sort them by the number of letters. We must distinguish between internal and external algorithms sorting. As for the sort of outer type - it uses a lot of memory, which certainly can not be said about the internal, also called sort in place, and it requires only a few bytes, and intermediate variables.

The main advantage of insertion sort algorithm is the effect on a small set of data, also on data sets that are already less sorted. This algorithm is robust and can sort the list as it is earned, which can sometimes be a very important point.

Now consider a simple insertion sort algorithm on the example of numbers. First of all, this algorithm was originally divided into two types - sorted and unsorted. Sorted part will have only one meaning - it is the first given number. The second number will be compared with the first, that is, from left to right, and so each next number in a given set should be compared with those that are left of it if the number is greater than they are swapped. As result, there is loop in insertion sort. Step by step, we compare and swap the number of checking them with previous ones.

Here is how it looks visually.

Take an arbitrary set of numbers and try to sort it.

The set of numbers: 3 1 8 5 2 9

We start comparing the first two numbers: 3 and 1

Clearly shows that the second than the first, then reshuffle and get a line of data: 1 3 8 5 2 9

Do the same thing with the other numbers, and obtain

1 3 8 5 2 9 - here we are left with a sequence of the same, as eight more in the previous two numbers;

1 3 5 8 2 9 - swapped 8 and 5;

1 2 3 5 8 9 - we have put in place the triple deuce and we see that in this our loop ends, as all the numbers are ordered from smallest to largest;

Consider the insertion sort algorithm in Java. At work we use an integrated development environment establishment, the company IBM platform Eclipse. To start, run it and create a new project. In short, after you run it, choose: File -> New and then choose Project. Next on the list, select the Android folder and the application for Android in it. After the selection, give the name for your project, and all other information as required.

So, we created a project for Android. Now we'll just program the algorithm. Find the file Main.Activity.java, it is located in the "src" folder. Here we see the typical preparation in which we will write our code. So, here is a program that we have: package com.example.test;

import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView;

public class MainActivity extends Activity {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

int[] d = {3, 1, 8, 5, 2, 9}; int k; int i, j;

for (i = 0; i < d.length; i++) { k = d[i];

for (j = i - 1; j >= 0 && d[j] > k; j--) { d[j + 1] = d[j]; }

d[j + 1] = k; }

String result = "";

for (i = 0; i < d.length; i++ ) { result = result + d[i] + " "; }

TextView MyTextView = (TextView) findViewById (R.id.label); MyTextView.setText(result); }

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

Our activity_main.xml looks like this: xmlns:tools="http://schemas.android.com/tools" android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent">

android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" tools:context=".MainActivity" />

After you run this program, you will see on the screen of your Android device the result as sorted list of numbers.

Android Emulator

Best regards, DYAMAR Engineering.

Language: 
Category: 
Platform: 

All rights reserved. You may not copy or distribute, electronically or otherwise, any part of this article for any purpose without prior written permission from the DYAMAR Engineering.