int max(int[] a, int m)
{
int n = a.length;
int t = a[0];
for(int i=1; i<n-m; i++)
if(a[i]>t) t = a[i];
return t;
}
int[] unknownSort(int[] a)
{
int n = a.length;
for(int i=n-1,j=0; i>0; i--,j++)
a[i] = max(a,j);
return a;
}
It is much faster than bubble sort, but a little slower than insertion sort.
That's not a sorting algorithm, it doesn't work.
Looks like a clumsy implementation of selection sort: http://en.wikipedia.org/wiki/Selection_sort
It will be a Selection Sort if you write it correctly. Your algrorithm will just corrupt the data in the array.
Related
I want to write this simple function in a way that makes the big O notation of it O(n^2), how can I make that possible ?
int getSum(int n){
int sum = (n*(n+1))/2;
return sum;
any ideas?
I'm not really sure why you want this, but you could do it with two nested loops:
int getSum(int n) {
int sum = 0;
for(int i = 1; i <= n; i++) {
int x = 0;
while(x++ < i) {
sum++;
}
}
return sum;
}
This runs 1+2+3+...+n times, which simplifies to (n^2+n)/2, hence O(n^2)
I would like to know what kind of sorting algorithm is the one below. I understand that it is a integer sorting algorithm but other than that I haven't figured it out:
void mySorter(int arr[]) {
int a = arr.length;
for (int i = 0; i < a-1; i++) {
int min = i;
for (int j = i +1 ; j < a; j++) {
if (arr[j] < arr[min])
min = j;
int temp = arr[min];
arr[min] = arr[i]
arr[i] = temp;
}
}
}
Could it be a selection sort?
It is Bubble Sort. Your code sorts the list in ascending order.
The question was given an array of n size with values ranging from [1-100]. Create a sorting algorithm and discuss it's time, space, and optimality. I don't have a decent understanding of asymptotic analysis and wasn't sure how to answer this.
Algorithm:
void sort(int a[], int n) {
int temp[100] = {0};
for(int i=0; i<n; i++)
temp[a[i]-1]++;
int c = 0;
for(int i=0; i<100; i++)
for(int j=0; j<temp[i]; j++) {
a[c] = i+1;
c++;
}
}
It's O(n), please refer Count Sort
Hello everyone i have a question. It's my task which one is below:
Let A[] be a natural numbers array of length N, which is partially sorted, i.e. there exists such index i(0 < i < N-1), that the subaray A[0],...,A[i] is incrementally sorted and also the subarray A[i+1],...,A[N] is incrementally sorted. Design the algorithm, which sorts the whole array A[] and works in place (so has space complexity O(1)) and the result must be stored in the same array A[]. Describe the algorithm, its correctness and its time complexity approximation.
For this question which approaching is better? Bubble sorting or Insertion sort? Or is there more effective solution? I prefered bubble sorting for this task but i am open to other opinions
static void bubbleSort(int arr[], int n)
{
int i, j, temp;
boolean swapped;
for (i = 0; i < n - 1; i++)
{
swapped = false;
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (swapped == false)
break;
}
}
static void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = { 1, 8, 45, 12, 22, 11, 90 };
int n = arr.length;
bubbleSort(arr, n);
System.out.println("Sorted array: ");
printArray(arr, n);
}
}
Bubble sort algorithm complexity is O(n^2). Even using if (swapped == false) break; this will not help to reduce the complexity (try for {2,3,4,5,1}, you will find out).
Since there exists such index i(0 < i < N-1), that the subaray A[0],...,A[i] is incrementally sorted and also the subarray A[i+1],...,A[N] is incrementally sorted.This problem can be solve in O(n) run time complexity. If we can find the index i where A[0:i] and A[i+1:n] are sorted, then we can think this problem as merging two sorted array into one array which can be done in O(n) time. Algorithm is given below:
void sortPartialSortedArray(int arr[], int n)
{
int pos = 0;
// find the position for which arr[0:pos] and arr[pos+1:n] is sorted
for(int i=0; i+1<n; i++) {
if(arr[i]>arr[i+1]) {
pos = i;
}
}
int i = pos, j= n-1;
// sort it from last position
while(i>=0 && j>=0) {
if(arr[i] > arr[j]) {
swap(arr[i],arr[j]);
}
j--;
if(i==j) {
i--;
}
}
}
With reference to Algorithm - Fourth Edition by Robert and Kevin, I am having difficulty in understanding the best case complexity for Insertion sort as per below code:
public class Insertion
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length;
for (int i = 1; i < N; i++)
{ // Insert a[i] among a[i-1], a[i-2], a[i-3]... ..
for (int j = i; j > 0 && less(a[j], a[j-1]); j--)
exch(a, j, j-1);
}
}
// See page 245 for less(), exch(), isSorted(), and main().
}
It says in the book that in best case (sorted array), the number of exchanges is 0 and number of compares is N-1. While I understood exchanges to be 0, I am having a hard time how can number of compares be N-1 in best case?
If the array is already sorted, then in the specific implementation of insertion-sort that you provide, each element will only be compared to its immediate predecessor. Since it's not less than that predecessor, the inner for-loop then aborts immediately, without requiring any further comparisons or exchanges.
Note that other implementations of insertion-sort do not necessarily have that property.
how can number of compares be N-1 in best case?
The best case happens when you have an already sorted array. The number of comparison is n-1 because the comparison is made from the 2nd element onwards till the last element.
This can also be observed from your given code:
for (int i = 1; i < N; i++) //int i=1 (start comparing from 2nd element)
The source code for the specific implementation is:
public class Insertion
{
public static void sort(Comparable[] a)
{ // Sort a[] into increasing order.
int N = a.length;
bool exc = false;
for (int i = 1; i < N; i++)
{ // Insert a[i] among a[i-1], a[i-2], a[i-3]... ..
for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
exch(a, j, j-1);
exc = true;
}
if (!exc)
break;
}
}
// See page 245 for less(), exch(), isSorted(), and main().
}