-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
60 lines (53 loc) · 1.36 KB
/
InsertionSort.java
File metadata and controls
60 lines (53 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.ArrayList;
/**
* Write a description of class InsertionSort here.
*
* @author Sofie Budman
* @version 2/14/2025
* Period 5
*/
public class InsertionSort
{
// instance variables - replace the example below with your own
private ArrayList<Integer>arr;
/**
* Constructor for objects of class InsertionSort
*/
public InsertionSort(ArrayList<Integer> arr)
{
this.arr = new ArrayList<Integer>();
for(int i: arr){
this.arr.add(i);
}
}
/**
* doInsertion Sort
*
* @param none
* @return number of swaps
*/
public int doInsertionSort()
{
int c = 0;
for (int i = 1; i < arr.size(); i ++){
int j = i;
while(j >0 && arr.get(j-1) > arr.get(j)){
int t = arr.set(j, arr.get(j-1));
arr.set(j-1, t);
j--;
c++;
}
}
return c;
}
public static void main(String[] args)
{
ArrayList array = new ArrayList<Integer>();
int[] oldArray = {1,-5,9,20,18,5,-7,0,2,8};
for(int num: oldArray)
array.add(num);
InsertionSort sort = new InsertionSort(array);
System.out.println(sort.doInsertionSort());
System.out.println("Number of swaps should be 23");
}
}