السلام عليكم ورحمة الله وبركاته
مطلوب مني اني اضع كودات 4 من خوارزميات البحث (Insertion,quick,counting,merage) في كود جافا داااخل interface
انا معي الكودات الاربعة بس مش عارفة اربطهم داخل interface بتمنى المساعدة منكم باسرع وقت بعد ادنكم
الكوداات في الاسفل وتجدوه ايضا مع المرفقات:-
//insertion
void sort(int a[]) throws Exception {
for (int i = 1; i < a.length; i++) {
int j = i;
int B = a;
while ((j > 0) && (a[j-1] > B)) {
if (stopRequested) {
return;
}
a[j] = a[j-1];
j--;
pause(i,j);
}
a[j] = B;
pause(i,j);
}
}
}
//MergeSort
class MergeSortAlgorithm extends SortAlgorithm {
void sort(int a[], int lo0, int hi0) throws Exception {
int lo = lo0;
int hi = hi0;
pause(lo, hi);
if (lo >= hi) {
return;
}
int mid = (lo + hi) / 2;
/*
* Partition the list into two lists and sort them recursively
*/
sort(a, lo, mid);
sort(a, mid + 1, hi);
/*
* Merge the two sorted lists
*/
int end_lo = mid;
int start_hi = mid + 1;
while ((lo <= end_lo) && (start_hi <= hi)) {
pause(lo);
if (stopRequested) {
return;
}
if (a[lo] < a[start_hi]) {
lo++;
} else {
/*
* a[lo] >= a[start_hi]
* The next element comes from the second list,
* move the a[start_hi] element into the next
* position and shuffle all the other elements up.
*/
int T = a[start_hi];
for (int k = start_hi - 1; k >= lo; k--) {
a[k+1] = a[k];
pause(lo);
}
a[lo] = T;
lo++;
end_lo++;
start_hi++;
}
}
}
void sort(int a[]) throws Exception {
sort(a, 0, a.length-1);
}
}
//QuickSort
class QSortAlgorithm extends SortAlgorithm {
void sort(int a[], int lo0, int hi0) throws Exception {
int lo = lo0;
int hi = hi0;
pause(lo, hi);
if (lo >= hi) {
return;
}
else if( lo == hi - 1 ) {
/*
* sort a two element list by swapping if necessary
*/
if (a[lo] > a[hi]) {
int T = a[lo];
a[lo] = a[hi];
a[hi] = T;
}
return;
}
/*
* Pick a pivot and move it out of the way
*/
int pivot = a[(lo + hi) / 2];
a[(lo + hi) / 2] = a[hi];
a[hi] = pivot;
while( lo < hi ) {
/*
* Search forward from a[lo] until an element is found that
* is greater than the pivot or lo >= hi
*/
while (a[lo] <= pivot && lo < hi) {
lo++;
}
/*
* Search backward from a[hi] until element is found that
* is less than the pivot, or lo >= hi
*/
while (pivot <= a[hi] && lo < hi ) {
hi--;
}
/*
* Swap elements a[lo] and a[hi]
*/
if( lo < hi ) {
int T = a[lo];
a[lo] = a[hi];
a[hi] = T;
pause();
}
if (stopRequested) {
return;
}
}
/*
* Put the median in the "center" of the list
*/
a[hi0] = a[hi];
a[hi] = pivot;
/*
* Recursive calls, elements a[lo0] to a[lo-1] are less than or
* equal to pivot, elements a[hi+1] to a[hi0] are greater than
* pivot.
*/
sort(a, lo0, lo-1);
sort(a, hi+1, hi0);
}
void sort(int a[]) throws Exception {
sort(a, 0, a.length-1);
}
}
//counting sort
import java.util.Arrays;
public static void countingSort(int[] a, int low, int high)
{
int[] counts = new int[high - low + 1]; // this will hold all possible values, from low to high
for (int x : a)
counts[x - low]++; // - low so the lowest possible value is always 0
int current = 0;
for (int i = 0; i < counts.length; i++)
{
Arrays.fill(a, current, current + counts, i + low); // fills counts elements of value i + low in current
current += counts; // leap forward by counts steps
}
}
الرجاااء المساااعدة باسرع وقت

