counter with upper and lower limit for a stepper motor code - arduino-uno

void CounterClockwise()
{
switch (step_number) {
case 0:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, HIGH);
break;
case 1:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, HIGH);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 2:
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, HIGH);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
case 3:
digitalWrite(STEPPER_PIN_1, HIGH);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
break;
}
step_number = ++step_number % 4;
for (counter; counter < 5; counter++)
{
delay(1000);
if (counter == 5) {
digitalWrite(STEPPER_PIN_1, LOW);
digitalWrite(STEPPER_PIN_2, LOW);
digitalWrite(STEPPER_PIN_3, LOW);
digitalWrite(STEPPER_PIN_4, LOW);
}
}
}
so im trying to make a counter for a motor.
it should be like the blinds, the blinds has a limit when it reached all the way to the top.
lets say from the bottom to the top takes 5seconds but when its somewhere in the middle than it should then less than 3seconds.
the for-loop is the limit. but as you can see it is wrong how the way it looks and ive been searching for ideas that acts like a limit.

Related

write a program that calculates the number of comparisons in binary insertion sorting?

I would like to know how I can write a program to calculate the number of comparison in binary insertion sorting
I tried to code the binary insertion program below, but I would like to know how I can calculate the overall comparisons made.
#include<iostream>
using namespace std;
int binarysearch (int a[], int sel, int high, int low){
int mid=(high+low)/2;
if(high<=low){
if(sel>a[high]){
return high+1;
}
else{
return high;
}
}
else{
if(sel==a[mid]){
return mid+1;
}
else if(sel>a[mid]){
return binarysearch( a, sel, high, mid+1);
}
else{
return binarysearch( a, sel, mid-1, low);
}
}}
void insertionsort(int a[], int n){
for(int i=1; i<n; i++){
int j=i-1;
int sel=a[i];
int loc=binarysearch(a,sel,j,0);
while(j>=loc){
a[j+1]=a[j];
j--;
}
a[j+1]=sel;
}
}
int main(){
int a[]= {1,6,2,5,3,4};
int n=sizeof(a)/sizeof(a[0]);
insertionsort(a,n);
cout<<"Sorted array is :";
for (int i = 0; i < n; i++)
cout<<a[i]<<"\t";
return 0;
}

Can we optimize randomized quicksort by tail recursion?

I know we can optimize quicksort by leveraging tail recursion by removing more than 1 recursion calls and reducing it to once single recursion call:-
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void quickSort(int arr[], int low, int high)
{
start:
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
low = pi+1;
high = high;
goto start;
}
}
But can we optimize randomized quicksort with tail recursion?
Tail stack recursion focuses on optimizing recursive calls. The only difference between randomized quicksort and normal quicksort is the partition function which selects a random pivot in case of randomized quicksort. Note that this partition function is non-recursive. As the recursive part of both randomized quicksort and normal quicksort is same, same optimization can be done in both cases. So, yes.

Is it normal for quicksort to be inefficient when sorting a completely descending array? [duplicate]

This question already has answers here:
Quick sort Worst case
(6 answers)
What is the worst case scenario for quicksort?
(6 answers)
Closed 4 years ago.
#include <iostream>
#include<stdio.h>
#include<fstream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main()
{
int arr[100000];
int i;
ifstream fin;
int n = 20000;
fin.open("reverse20k.txt");
if(fin.is_open())
{
for(i=0;i<n;i++)
fin>>arr[i];
}
quickSort(arr, 0, n-1);
return 0;
}
It takes this about 1.25 seconds to sort a 20k purely descending array, while it takes merge sort only 0.05. Is quick sort just extremely inefficient when sorting descending arrays, or is there just something wrong with the algorithm?

Finding element of a given rank in an arbitrary array

What I am is doing is using a quicksort algorithm, so that my pivot element(which will always be the first element of the array gets positioned to its appropriate position in the sorted array and I am calling this method again until I do not position the element at a given rank. Is there a better solution?
Here is my code:
public static int arbitrary(int a[],int x,int y,int rank)//x and y are first and last indecies of the array
{
int j=y,temp;
if(x<y)
{
for(int i=y;i>x;i--)
{
if(a[i]>a[x])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j--;
}
}
temp=a[x];
a[x]=a[j];
a[j]=temp;
//System.out.println("j is "+j);
if(j==rank)
return a[j];
else if(rank<j)
return arbitrary(a,x,j-1,rank);
else
return arbitrary(a,j+1,y,rank);
}
else
return 0;
}
The algorithm you have implemented is called Quickselect.
Just select a random pivot and to get rid of the worst case with O(n²) time complexity.
The expected runtime is now about 3.4n + o(n).
Quickselect is probably the best tradeoff between performance and simplicity.
An even more advanced pivot selection strategy results in 1.5n + o(n) expected time
(Floyd-Rivest Algorithm).
Fun Fact: With deterministic algorithms you can't go better than 2n. BFPRT for example needs about 2.95n to select the median.
Best way to find Rank element by using the QuickSort method:
In QuickSort, at every iteration you are able to get fixed one pivot element.
When the RankElement == PivotIndex, and break the condition and return the value.
public class FindRank {
public void find(int[] arr, int low, int high, int k) {
if (low < high) {
int pivot = partition(arr, low, high, k);
find(arr, low, pivot - 1, k);
find(arr, pivot + 1, high, k);
}
}
public int partition(int[] arr, int low, int high, int k) {
int pivotIndex = high;
while (low < high) {
while (low < high && arr[low] <= arr[pivotIndex]) {
low++;
}
while (low > high && arr[high] >= arr[pivotIndex]) {
high--;
}
if (low < high) {
swap(arr, low, high);
}
}
swap(arr, pivotIndex, high);
if (pivotIndex == k) {
System.out.println("Array Value:" + arr[k] + " index:" + k);
return k;
}
return high;
}
private void swap(int[] arr, int low, int high) {
int temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
}
}

Quick sort logic

I am trying to implement quick sort in java and I have one doubt. So here's my quick sort code:
package com.sorting;
public class QuickSort implements Sort {
#Override
public int [] sort(int[] arr) {
return quickSort(arr, 0, arr.length - 1);
}
private int [] quickSort(int[] numbers, int low, int high) {
if (low < high) {
int q = partitionTheArrayAroundPivot(numbers, low, high);
if (low < q)
quickSort(numbers, low, q);
if ((q+1) < high)
quickSort(numbers, q + 1, high);
}
return numbers;
}
private int partitionTheArrayAroundPivot(int[] numbers, int low, int high) {
int pivot = selectPivot(numbers, low, high);
int i = low;
int j = high;
while (true) {
while (numbers[i] < pivot) {
i++;
}
while (numbers[j] > pivot) {
j--;
}
if ( i <= j) {
swap(numbers, i, j);
i++;
j--;
} else {
return j;
}
}
}
private int selectPivot(int[] numbers, int low, int high) {
return numbers[high];
}
private void swap(int[] numbers, int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
Doubt 1: We keep increasing the index i till we hit a number which is >= pivot
while (numbers[i] < pivot)
i++;
Similarly we keep decreasing the index j till we hit a number which is <= pivot
while (numbers[j] > pivot)
j--;
So, this means that both indexes will also come out of the loop if both hits pivots at two different places e.g. 1,0,1 here if pivot is 1, then i will be 0 and j will be 2. And the below condition will be satisfied
if (i <= j) {
....
}
but in that case it won't be able to sort the above array (1,0,1) because after swapping we are increasing i and decreasing j so the value become i = j = 1. After that i will hit the third element i.e 1 and will again come out of the loop with value i = 2 and similarly j = 0 and we will not be able to sort the array.
So where's the problem? Am I missing something?
I would rewrite the code a little so that selectPivot returns the index instead:
private int selectPivotIndex(int[] numbers, int low, int high) {
return high;
}
Then the partitioning funcion can move the pivot aside, and sort the remaining items according to pivot value. A single loop will do it, in this implementation duplicate pivots will end up on right side:
private int partitionTheArrayAroundPivot(int[] numbers, int low, int high) {
int pivotIndex = selectPivotIndex(numbers, low, high);
swap(numbers, pivotIndex, high); // Not needed if selectPivotIndex always returns high
int newPivotIndex = low;
for(int i = low; i < high; i++)
{
if(numbers[i] < numbers[pivotIndex])
{
swap(numbers, i, newPivotIndex);
newPivotIndex++;
}
}
swap(numbers, newPivotIndex, pivotIndex);
return newPivotIndex;
}
Finally, a small adjustment needs to be done in the quickSort method so that we don't end up in an eternal loop:
if (low < q)
quickSort(numbers, low, q - 1);
This approach is IMHO easier to understand and debug, hope it works for you.
Use
while (numbers[i] <= pivot) and
while (numbers[j] >= pivot) and your code will work

Resources