Java-Processing-Search and Sort

Write the code for the following methods:
Selection sort is to repetitively pick up the smallest element and put it into the right position:
Find the smallest element, and put it to the first position.
Find the next smallest element, and put it to the second position.
Repeat until all elements are in the right positions.

Insertion sort maintains a sorted sub-array, and repetitively inserts new elements into it. The process is as following:
Take the first element as a sorted sub-array.
Insert the second element into the sorted sub-array (shift elements if needed).
Insert the third element into the sorted sub-array.
Repeat until all elements are inserted.

Bubble sort repetitively compares adjacent pairs of elements and swaps if necessary.
Scan the array, swapping adjacent pair of elements if they are not in relative order. This bubbles up the largest element to the end.
Scan the array again, bubbling up the second largest element.
Repeat until all elements are in order.

Recursive Sort Algorithms
Quick Sort
Quick sort is to partition the array around one element and then sort each part recursively.
Pick up one element as the pivot
Move all elements less than the pivot to the left, and all elements greater than the pivot to the right
Apply the above steps on both parts

Merge Sort
Consider the problem of sorting an array of numbers in order. Merge sort is to recursively divide the array in half, and then recombine the sorted subarrays.
Divide the array in half.
Recursively divide until each subarray has only one element.
Merge the sorted subarrays.

Use the following algorithms/pseudo code as a starting point:
int search(int [] arr, int item)
{
For (int i = 0; i< arr.length; i++)
    {
        If (item == arr[i])
        {
            Return i;
        }
}
  return -1;
}

Void bubbleSort(int[] arr)
{
  Int swaps = 1;
    While (swaps > 0)
{
  swaps=0;
    For ()
      If (arr[i]> arr[i+1])
{
          swap(arr, i, i+1);
swaps++;
}
}
}

void quickSort(int [] arr, int low, int high) //recursive method
{
  Int pivot = (low+high)/2;
  If (high <=  low)
        return;
  // put the higher values above pivot – move pivot down

// put the lower values below pivot – move the pivot up

If (pivot > low)
  quickSort(arr, low, pivot-1);
If (pivot < high)
  quickSort(arr, pivot+1, high);
}

void mergeSort(int[] arr, int low, int high) //recursive method
{
  Int pivot = (low+high)/2;
  If (pivot > low)
    mergeSort(arr, low, pivot);
  If (pivot < high)
    mergeSort(arr, pivot+1, high);

// merge 2 arrays

}

Void swap( int[] arr, int to, int from)
{
    Int temp = arr[from];
    Arr[from] = arr[to];
    Arr[to] = temp;

}

void selectSort(int [] arr)
{
    for(int i=0; i< arr.length; i++)
{
  Int min = i;
    For (int j=i+1; j<arr.length-1; j++)
      If (arr[min] > arr[j])
          Min = j;
  swap(arr, i, min);}
}

void insertSort(int[] arr)
{
    For (i=1; i <arr.length; i++)
{
      For (j=0; j<i; j++)
        {
            If (arr[i] < arr[j])
                shiftUp(arr, arr[i], j, i) ;

}

}

}

int binSearch(int [] arr, int item)
{
  return binSearch(arr, item, 0, arr.length-1);
}

int binSearch(int [] arr, int item, int low, int high)
{
  int pivot = (low+high)/2;
If (arr[pivot] == item)
Return pivot;
If (arr[pivot] < item)
binSearch(arr, item, low, pivot-1);
else
binSearch(arr, item, pivot+1, high);
  return -1;
}

void shiftUp(int[]arr, int value, int pos, int end)
{
    For (i=end; i>pos; i–)
{
    Arr[i] = arr[i-1];
}
Arr[pos] = value;
}

The result should show a comparison between the different methods and the speed at which the result is outputted.

Leave a Reply

Your email address will not be published. Required fields are marked *