Wednesday, December 9, 2020

insertion sort algoritham

                             Insertion Sort


Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.


 What is Binary Insertion Sort?

We can use binary search to reduce the number of comparisons in normal insertion sort. Binary Insertion Sort uses binary search to find the proper location to insert the selected item at each iteration. In normal insertion, sorting takes O(i) (at ith iteration) in worst case. We can reduce it to O(logi) by using binary search. The algorithm, as a whole, still has a running worst case running time of O(n2) because of the series of swaps required for each insertion. Refer this for implementation.

How to implement Insertion Sort for Linked List?

create an empty sorted list
transverse the given list
insert current node in sorted way
change head of given linked list to head of sorted list

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.




#include <stdlib.h> #include <iostream> using namespace std; void insertionSort(int arr[], int length); void printArray(int array[], int size); int main() { int array[5] = {5, 1, 6, 2, 4, 3}; insertionSort(array, 6); return 0; } void insertionSort(int arr[], int length) { int i, j, key; for (i = 1; i < length; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { key = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = key; j--; } } cout << "Sorted Array: "; printArray(arr, length); } void printArray(int array[], int size) { int j; for (j = 0; j < size; j++) { cout <<" "<< array[j]; } cout << endl;
}







How Insertion Sort Works?

Following are the steps involved in insertion sort:

  1. We start by making the second element of the given array, i.e. element at index 1, the key. The key element here is the new card that we need to add to our existing sorted set of cards(remember the example with cards above).
  2. We compare the key element with the element(s) before it, in this case, element at index 0:
    • If the key element is less than the first element, we insert the key element before the first element.
    • If the key element is greater than the first element, then we insert it after the first element.
  3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position.
  4. And we go on repeating this, until the array is sorted.







Sunday, November 29, 2020

MarketBasket

 Market basket analysis is a data miningtechnique used by retailers to increase sales by better understanding customer purchasing patterns. It involves analyzing large data sets, such as purchase history, to reveal product groupings, as well as products that are likely to be purchased together.

The adoption of market basket analysis was aided by the advent of electronic point-of-sale (POS) systems. Compared to handwritten records kept by store owners, the digital records generated by POS systems made it easier for applications to process and analyze large volumes of purchase data.

Implementation of market basket analysis requires a background in statistics and data science, as well as some algorithmic computer programming skills. For those without the needed technical skills, commercial, off-the-shelf tools exist.

One example is the Shopping Basket Analysis tool in Microsoft Excel, which analyzes transaction data contained in a spreadsheet and performs market basket analysis. The items to be analyzed must be related by a transaction ID. The Shopping Basket Analysis tool then creates two worksheets: the Shopping Basket Item Groups worksheet, which lists items that are frequently purchased together, and the Shopping Basket Rules worksheet, which shows how items are related (For example, purchasers of Product A are likely to buy Product B).

Types of market basket analysis

There are two types of market basket analysis:

  1. Predictive market basket analysis: This type considers items purchased in sequence to determine cross-sell
  2. Differential market basket analysis: This type considers data across different stores, as well as purchases from different customer groups during different times of the day, month or year. If a rule holds in one dimension (like store, time period or customer group), but does not hold in the others, analysts can determine the factors responsible for the exception. These insights can lead to new product offers that drive higher sales.

insertion sort algoritham

                              Insertion Sort Insertion sort is a simple sorting algorithm that works similar to the way you sort playing car...