Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita009cca

Browse files
Merge pull requestTheAlgorithms#85 from varunu28/master
Updated Insertion Sort
2 parentse414753 +96af9a7 commita009cca

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

‎Sorts/BubbleSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BubbleSort
99
/**
1010
* This method implements the Generic Bubble Sort
1111
*
12-
* @param array The array tomake the binary search
12+
* @param array The array tobe sorted
1313
* @param last The count of total number of elements in array
1414
* Sorts the array in increasing order
1515
**/

‎Sorts/InsertionSort.java

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
1-
importjava.util.Scanner;
2-
31
/**
4-
* This class implements Insertion Sort
5-
*
6-
* @author Unknown
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
74
*
85
*/
9-
classInsertionSort
10-
{
11-
/**
12-
* Main Method
13-
*
14-
* @param args Command line arguments
15-
*/
16-
publicstaticvoidmain(String[]args)
17-
{
18-
intarray[]=newint[6];
19-
Scannerinput=newScanner(System.in);
20-
21-
//Input
22-
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
23-
for(inti=0;i<6;i++)
24-
{
25-
array[i]=input.nextInt();
26-
}
27-
28-
//Sorting
29-
for(inti=0;i<6;i++)
30-
{
31-
inttemp=array[i];
32-
intj=i-1;
33-
while(j>=0 &&temp<array[j] )
34-
{
35-
array[j+1]=array[j];
36-
j--;
37-
}
38-
39-
array[j+1]=temp;
40-
}
41-
42-
//Output
43-
for(inti=0;i<6;i++)
44-
{
45-
System.out.print(array[i]+"\t");
46-
}
47-
input.close();
48-
}
6+
7+
classInsertionSort {
8+
9+
/**
10+
* This method implements the Generic Insertion Sort
11+
*
12+
* @param array The array to be sorted
13+
* @param last The count of total number of elements in array
14+
* Sorts the array in increasing order
15+
**/
16+
17+
publicstatic <TextendsComparable<T>>voidIS(Tarray[],intlast) {
18+
Tkey;
19+
for (intj=1;j<last;j++) {
20+
21+
// Picking up the key(Card)
22+
key =array[j];
23+
inti =j-1;
24+
while (i>=0 &&key.compareTo(array[i]) <0) {
25+
array[i+1] =array[i];
26+
i--;
27+
}
28+
// Placing the key (Card) at its correct position in the sorted subarray
29+
array[i+1] =key;
30+
}
31+
}
32+
33+
// Driver Program
34+
publicstaticvoidmain(String[]args) {
35+
// Integer Input
36+
int[]arr1 = {4,23,6,78,1,54,231,9,12};
37+
intlast =arr1.length;
38+
Integer[]array =newInteger[arr1.length];
39+
for (inti=0;i<arr1.length;i++) {
40+
array[i] =arr1[i];
41+
}
42+
43+
IS(array,last);
44+
45+
// Output => 1 4 6 9 12 23 54 78 231
46+
for (inti=0;i<array.length;i++) {
47+
System.out.print(array[i] +" ");
48+
}
49+
System.out.println();
50+
51+
// String Input
52+
String[]array1 = {"c","a","e","b","d"};
53+
last =array1.length;
54+
55+
IS(array1,last);
56+
57+
//Output => abcde
58+
for(inti=0;i<last;i++)
59+
{
60+
System.out.print(array1[i]+"\t");
61+
}
62+
}
4963
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp