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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
using namespace std;
//function declaration
void printArray(int arr[], int size);
int insertion_sort(int arr[], int n);
//int binaryS(int arr[], int key);
// Driver program to test above functions
int main(){
int key;
int size;
cout<<"Enter size of list (between 1 and 25): ";
cin>>size;
if (size <= 0 || size > 25)//if user's input is less then zero, an error is given
{
cout<<"ERROR: you entered an incorrect value for the array size!"<<endl;
return 0;
}
int *arr = new int[size]; // creating an array of size equal to the input of the user
for(int i=0; i<size; i++)
{
cout<<"Enter one list element: ";
cin>>arr[i];
}
cout<<"Content of list: ";
printArray(arr, size);
int temp = insertion_sort(arr, size); //calling sorting algo
cout<<"Content of list: ";
printArray(arr, size);
cout << "Enter a key to search for: " << endl;
cin >> key;
//binaryS(arr,size,key);
}
/*
}
binaryS(*arr, size, key);
}
int binaryS(int arr[], int key, int left, int right) {
while (left <= right) {
int middle = (left + right) / 2;
if (arr[middle] == key)
return middle;
else if (arr[middle] > key)
right = middle - 1;
else
left = middle + 1;
}
return -1;
}
*/
int insertion_sort (int arr[], int length){
int j,temp;
for (int i = 0; i < length; i++){
j = i;
while (j > 0 && arr[j] < arr[j-1]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
return 0;
}
/* Function to print an array */
void printArray(int arr[], int size){
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
//}
|