Com Sci mod 1 Flashcards

1
Q

What is a stack?

A

A stack is a linear data structure in which it inserts with push and deletes with pop which are allowed only at the end, called the top of the stack.
LIFO-(last in first out)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the Push operation do?

A

it inserts data at the top of the stack
the data entered immediately becomes the top of the stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the Pop operator do?

A

Deletes the last inserted element from the stack

Always deletes the top element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the isEmpty operation do?

A

Returns TRUE if the stack is empty else it returns FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the isFull operation do?

A

Returns TRUE if the stack is full, else returns FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a queue?

A

A queue is a linear data structure which operates in a FIFO- (First In First Out) manner

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the enqueue operation do?

A

Adds data to the rear end of the queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the dequeue operation do?

A

Removes elements from the head (front) of the queue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a linear search?

A

Linear searches iterates through a collection one element at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a disadvantage for a linear search?

A

It is slow for large data sets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an advantage of a linear search?

A
  • Fast for searches of small to medium data sets
  • Does not need to be sorted
  • Useful for data structures that do not have random access (linked lists)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is binary search?

A

Binary search is a search algorithm that finds the position of a target value within a sorted array. Half of the array is eliminated during each “step”. if the mean is not the target value and if the mean is smaller than the target value it continues to the right until the target value is found. if the mean is larger than the target value it continues to the left until the target value is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a selection sort?

A

search through an array and keep track of the minimum value during each iteration. At the end of each iteration, we swap values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is bubble sorting?

A

pairs of adjacent numbers are compared, and the elements are swapped if they are not in order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does a bubble sort algorithm look like?

A

include <stdio.h></stdio.h>

int main(void)
{
int a[] = {7,1,3,9,0,2,4,5,8,6};

int length =10;
for (int i = 0; i< length; i++)
{
for (int j =0; j< (length-1); j++)
// < for ascending order and > for descending order
{
if (a[j] > a[j + 1])
{
int temp= a[j];
a[j] = a[ j + 1];
a[j + 1] = temp;
}
}
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does a selection sort algorithm look like?

A

include <stdio.h></stdio.h>

int main()
{
int a[] = {5, 9, 7, 6, 4, 0, 2, 3, 8, 1};
int length = 10;

for (int i = 0; i < length; i ++)
{
int min_pos = i;
for (int j = i + 1; j < length; j ++)
if (a[j] < a [min_pos]) min_pos = j;

// to make it go from largest to smallest change < to >

if (min_pos != i)
{
int temp = a[i];
a[i] = a[min_pos];
a[min_pos] = temp;
}
}
for (int i = 0; i < length; i ++)
printf(“a[%d] = %d/n”, i, a[i]);

return 0;
}

17
Q

What does a binary search algorithm look like?

A

include <stdio.h></stdio.h>

int binarySearch(int array[], int x, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf(“Not found”);
else
printf(“Element is found at index %d”, result);
return 0;
}

18
Q

What does a linear search algorithm look like?

A

include <stdio.h></stdio.h>

int num=0, int arr[], int key=0,c=0)
void main() {
printf(“ please answer the value you are searching for”);
scanf (“%d,& key”);
for (c=0; c<100;c++)
{
if (arr [c]==key){
printf(“found”);
}
}

19
Q

what does the push function look like?

A

int push(int data){
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf(“Could not insert data, Stack is full.\n”);
}
}

20
Q

what does the pop function look like?

A

int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf(“Could not retrieve data, Stack is empty.\n”);
}
}

21
Q

What does isEmpty look like?

A

int isempty(){
if(top == -1)
return 1;
else
return 0;

22
Q

What does isFull look like?

A

int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;

23
Q

What does ENQUEUE look like?

A

include<stdio.h></stdio.h>

#define SIZE 5

int front =-1
int rear =-1//queue is empty
void enqueue(int Q[SIZE],int item)
{
if (rest<SIZE-1)
{
rear++;
Q[rear] = item;
}
else{
printf(“Queue is full”);
}
if (front==-1)
{
front ++
}
}

24
Q

What does DEQUEUE look like?

A

include<stdio.h></stdio.h>

#define SIZE 5

int front =-1
int rear =-1//queue is empty
void dequeue(int Q[SIZE],int item)
{
if (front<=rear)
{
printf(“\nDeleted item is %d”, Q[front]);
front++
}
else{
printf(“Queue is empty”);
front =-1;
rear =-1;
}
}

25
Q

What are Abstract Data Types

A

A way of storing and manipulating data by the use of a data structure and a set of predefined functions to carry out the operations that manipulate it