Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

116
LINES

< > BotCompany Repo | #1029416 // timSortIntArrayWithComparator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (157L/2K).

1  
scope timSortIntArrayWithComparator.
2  
3  
// not actually Timsort, but works well
4  
5  
// This code has been contributed by 29AjayKumar 
6  
// from: https://www.geeksforgeeks.org/timsort/
7  
8  
static final int #RUN = 32; 
9  
10  
// this function sorts array from left index to  
11  
// to right index which is of size atmost RUN  
12  
svoid #insertionSort(int[] arr, IntComparator comparator, int left, int right) { 
13  
    for (int i = left + 1; i <= right; i++)  
14  
    { 
15  
        int temp = arr[i]; 
16  
        int j = i - 1; 
17  
        while (j >= left && comparator.compare(arr[j], temp) > 0)
18  
        { 
19  
            arr[j + 1] = arr[j]; 
20  
            j--; 
21  
        } 
22  
        arr[j + 1] = temp; 
23  
    } 
24  
} 
25  
26  
// merge function merges the sorted runs  
27  
svoid #merge(int[] arr, IntComparator comparator, int l, int m, int r) { 
28  
    // original array is broken in two parts  
29  
    // left and right array  
30  
    int len1 = m - l + 1, len2 = r - m; 
31  
    int[] left = new int[len1]; 
32  
    int[] right = new int[len2]; 
33  
    for (int x = 0; x < len1; x++)  
34  
    { 
35  
        left[x] = arr[l + x]; 
36  
    } 
37  
    for (int x = 0; x < len2; x++)  
38  
    { 
39  
        right[x] = arr[m + 1 + x]; 
40  
    } 
41  
42  
    int i = 0; 
43  
    int j = 0; 
44  
    int k = l; 
45  
46  
    // after comparing, we merge those two array  
47  
    // in larger sub array  
48  
    while (i < len1 && j < len2)  
49  
    { 
50  
        if (comparator.compare(left[i], right[j]) <= 0)
51  
        { 
52  
            arr[k] = left[i]; 
53  
            i++; 
54  
        } 
55  
        else 
56  
        { 
57  
            arr[k] = right[j]; 
58  
            j++; 
59  
        } 
60  
        k++; 
61  
    } 
62  
63  
    // copy remaining elements of left, if any  
64  
    while (i < len1) 
65  
    { 
66  
        arr[k] = left[i]; 
67  
        k++; 
68  
        i++; 
69  
    } 
70  
71  
    // copy remaining element of right, if any  
72  
    while (j < len2)  
73  
    { 
74  
        arr[k] = right[j]; 
75  
        k++; 
76  
        j++; 
77  
    } 
78  
} 
79  
80  
// iterative Timsort function to sort the  
81  
// array[0...n-1] (similar to merge sort)  
82  
svoid timSortIntArrayWithComparator(int[] arr, int n default lIntArray(arr), IntComparator comparator) { 
83  
    // Sort individual subarrays of size RUN  
84  
    for (int i = 0; i < n; i += RUN)  
85  
    { 
86  
        insertionSort(arr, comparator, i, Math.min((i + 31), (n - 1))); 
87  
    } 
88  
89  
    // start merging from size RUN (or 32). It will merge  
90  
    // to form size 64, then 128, 256 and so on ....  
91  
    for (int size = RUN; size < n; size = 2 * size)  
92  
    { 
93  
      ifdef timSort_debug
94  
        print("size=" + size);
95  
      endifdef
96  
          
97  
        // pick starting point of left sub array. We  
98  
        // are going to merge arr[left..left+size-1]  
99  
        // and arr[left+size, left+2*size-1]  
100  
        // After every merge, we increase left by 2*size  
101  
        for (int left = 0; left < n; left += 2 * size)  
102  
        { 
103  
              
104  
            // find ending point of left sub array  
105  
            // mid+1 is starting point of right sub array  
106  
            int mid = Math.min(left + size - 1, n - 1);
107  
            int right = Math.min(left + 2 * size - 1, n - 1); 
108  
109  
            // merge sub array arr[left.....mid] &  
110  
            // arr[mid+1....right]  
111  
            merge(arr, comparator, left, mid, right); 
112  
        } 
113  
    } 
114  
}
115  
116  
end scope

Author comment

Began life as a copy of #1029412

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029416
Snippet name: timSortIntArrayWithComparator
Eternal ID of this version: #1029416/4
Text MD5: 470ae87db6575130c460f5565e1af998
Transpilation MD5: 4d2ac067974fad06a7c423527e144db7
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-12 13:07:57
Source code size: 3286 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 322 / 612
Version history: 3 change(s)
Referenced in: [show references]